我正在关注 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)
我用谷歌搜索了这个,但没有得到解决方案