0

我正在关注 gson-android 的本教程

http://www.javacodegeeks.com/2011/01/android-json-parsing-gson-tutorial.html

我的json是

{"IsAuthenticated":true,"BusinessId":57,"Success":true,"ErrorId":0}

我已经创建了我的模型对象:

登录响应.java

import com.google.gson.annotations.SerializedName;
import com.xxxx.xxxx.R.string;

public class LoginResponse {

    @SerializedName("Sucess")
    private Boolean sucess;

    @SerializedName("IsAuthenticated")
    private Boolean isAuthenticated;

    @SerializedName("BusinessId")
    private String businessId;

    @SerializedName("ErrorId")
    private string errorId;

    public Boolean getSucess() {
        return sucess;
    }

    public void setSucess(Boolean sucess) {
        this.sucess = sucess;
    }

    public Boolean getIsAuthenticated() {
        return isAuthenticated;
    }

    public void setIsAuthenticated(Boolean isAuthenticated) {
        this.isAuthenticated = isAuthenticated;
    }

    public String getBusinessId() {
        return businessId;
    }

    public void setBusinessId(String businessId) {
        this.businessId = businessId;
    }

    public string getErrorId() {
        return errorId;
    }

    public void setErrorId(string errorId) {
        this.errorId = errorId;
    }

}

运行此代码后,我的应用程序正在强制关闭。

LOGCAT

    07-04 12:22:40.682: E/AndroidRuntime(2694): FATAL EXCEPTION: AsyncTask #3
07-04 12:22:40.682: E/AndroidRuntime(2694): java.lang.RuntimeException: An error occured while executing doInBackground()
07-04 12:22:40.682: E/AndroidRuntime(2694):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.lang.Thread.run(Thread.java:856)
07-04 12:22:40.682: E/AndroidRuntime(2694): Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 67
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.Gson.fromJson(Gson.java:803)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.Gson.fromJson(Gson.java:741)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity.ValidateUser(LoginActivity.java:300)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity.access$0(LoginActivity.java:276)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity$UserLoginTask.doInBackground(LoginActivity.java:222)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.example.xxxx.loginActivity$UserLoginTask.doInBackground(LoginActivity.java:1)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
07-04 12:22:40.682: E/AndroidRuntime(2694):     ... 4 more
07-04 12:22:40.682: E/AndroidRuntime(2694): Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 67
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374)
07-04 12:22:40.682: E/AndroidRuntime(2694):     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165)
07-04 12:22:40.682: E/AndroidRuntime(2694):     ... 14 more

..我就是这样做的

    HttpResponse response = client.execute(httpGet);

    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(content));


        Gson gson = new Gson();
        LoginResponse myResponse = gson.fromJson(reader, LoginResponse.class);
        Log.d(TAG, myResponse.getBusinessId().toString());



    } else {
        Log.e(TAG, "Failed to download file");
    }

难道我做错了什么?我只是跟着 tut 里的东西。任何人都可以帮助我。

更新:

在将字符串更正为字符串和其他内容后,我能够修复应用程序崩溃。我仍然无法得到对象。

更新的日志在这里

07-08 05:02:53.625: W/System.err(869): org.json.JSONException: End of input at character 0 of 
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONTokener.nextValue(JSONTokener.java:97)
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONObject.<init>(JSONObject.java:154)
07-08 05:02:53.656: W/System.err(869):  at org.json.JSONObject.<init>(JSONObject.java:171)
07-08 05:02:53.656: W/System.err(869):  at com.example.xxxx.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:238)
07-08 05:02:53.656: W/System.err(869):  at com.example.xxxx.LoginActivity$UserLoginTask.onPostExecute(LoginActivity.java:1)
07-08 05:02:53.656: W/System.err(869):  at android.os.AsyncTask.finish(AsyncTask.java:631)
07-08 05:02:53.656: W/System.err(869):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-08 05:02:53.656: W/System.err(869):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
07-08 05:02:53.675: W/System.err(869):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 05:02:53.675: W/System.err(869):  at android.os.Looper.loop(Looper.java:137)
07-08 05:02:53.675: W/System.err(869):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 05:02:53.675: W/System.err(869):  at java.lang.reflect.Method.invokeNative(Native Method)
07-08 05:02:53.675: W/System.err(869):  at java.lang.reflect.Method.invoke(Method.java:511)
07-08 05:02:53.675: W/System.err(869):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 05:02:53.675: W/System.err(869):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 05:02:53.675: W/System.err(869):  at dalvik.system.NativeStart.main(Native Method)

我用谷歌搜索了这个,但没有得到解决方案

4

4 回答 4

1

您的

   @SerializedName("BusinessId")
    private String businessId;

    @SerializedName("ErrorId")
    private string errorId;

应该是整数

  @SerializedName("BusinessId")
    private int businessId;

    @SerializedName("ErrorId")
    private int errorId;
于 2013-07-04T12:54:22.327 回答
0

您的 LoginResponse.java 中有一个错字:

@SerializedName("Sucess")

应该

@SerializedName("Success")

也许还有更多问题,您能否检查一下您尝试解析的 JSON 是否确实是您所期望的?

于 2013-07-04T12:36:34.827 回答
0

真的private string errorId;有小写吗?您可能想要String, or int(两者都可以)。

gson 中的反射内容会读取您的字段类型,并认为它必须是嵌套 json 对象可以映射到的对象。可以解释一下Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was NUMBER at line 1 column 67:你的反射映射器期待一个对象(但不是java.lang.String)类型,ErrorId因为你给了它一些它认为是另一种神秘对象的东西stringString但是,它是一个知道如何处理的类。

于 2013-07-04T12:41:19.220 回答
0

您从 Gson 收到的错误是告诉您它期望{在 JSON 的第 67 列(字符)处找到 a。这意味着errorId是 Java 类中的对象类型,而不是数字(例如int, long)或 a String(请参阅下面关于自动String转换的最后一段)。

查看您的代码,您有...

@SerializedName("ErrorId")
private string errorId;

显然你已经创建了一个string类(小写s),已经使用了它,并且 Gson 正试图反序列化它。这就是为什么你永远不应该这样做。首先,类是Java应该以大写字母开头。其次,您永远不应该创建与系统名称相同的类。(选项 B 是您显示的 Java 类不是实际运行的,并且那里还有一些其他对象类型)。

如果您通过将其更改为 a 来解决此问题,String则错误将消失。尽管errorId(and businessId) 现在String在您的 Java 中是 a,但 Gson 实际上会自动将 JSON 中的数字类型转换String为 Java 类中的类型。您还需要修复其中的拼写错误,sucess否则它将最终成为null因为 JSON 中不存在该错误,并且 Gson 会默默地忽略任何丢失的元素。

于 2013-07-05T15:11:31.093 回答