我正在开发应用程序,在该应用程序中我使用 Gson 解析来序列化包含一些数据数组的 json 对象..但是这里我解析的方法存在一些问题......它没有让我成功解析..我从未尝试过解析带有序列化注释的 Gson 库的 json 数据。你能告诉我我的错误在哪里吗?
这是代码..
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
GetAllAssetsResult response = gson.fromJson(reader, GetAllAssetsResult.class);
private InputStream retrieveStream(String url) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(url);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
return getResponseEntity.getContent();
}
catch (IOException e) {
getRequest.abort();
Log.w(getClass().getSimpleName(), "Error for URL " + url, e);
}
return null;
}
这是序列化注释基础 Pojo:
package com.DemoGsonLib;
import java.util.List;
import com.google.gson.annotations.SerializedName;
public class GetAllAssetsResult {
public List<GetAllAssetsResult> getAllAssetsResult;
@SerializedName("ATCOCode")
public String ATCOCode;
@SerializedName("AssetID")
public String AssetID;
@SerializedName("Bearing")
public String Bearing;
@SerializedName("CommonName")
public String CommonName;
@SerializedName("Locality")
public String Locality;
@SerializedName("Location")
public String Location;
@SerializedName("SMSNumber")
public String SMSNumber;
@SerializedName("Street")
public String Street;
}
json数据:
{
"GetAllAssetsResult": [
{
"ATCOCode": null,
"AssetID": 1,
"Bearing": null,
"CommonName": null,
"Locality": null,
"Location": null,
"SMSNumber": "Little Clare",
"Street": null
},
{
"ATCOCode": "34000000002",
"AssetID": 2,
"Bearing": null,
"CommonName": "Gloucester Green Bus Station",
"Locality": "Oxford City Centre",
"Location": "50.4913",
"SMSNumber": "oxfagwdw",
"Street": "Gloucester Green"
},
{
错误
11-16 13:16:26.805: WARN/System.err(1762): java.lang.NullPointerException
11-16 13:16:26.805: WARN/System.err(1762): at com.DemoGsonLib.DemoGsonLibActivity.onCreate(DemoGsonLibActivity.java:39)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.Activity.performCreate(Activity.java:4533)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-16 13:16:26.805: WARN/System.err(1762): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 13:16:26.805: WARN/System.err(1762): at android.os.Looper.loop(Looper.java:137)
11-16 13:16:26.805: WARN/System.err(1762): at android.app.ActivityThread.main(ActivityThread.java:4517)
11-16 13:16:26.805: WARN/System.err(1762): at java.lang.reflect.Method.invokeNative(Native Method)
11-16 13:16:26.805: WARN/System.err(1762): at java.lang.reflect.Method.invoke(Method.java:511)
11-16 13:16:26.805: WARN/System.err(1762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
11-16 13:16:26.805: WARN/System.err(1762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
11-16 13:16:26.805: WARN/System.err(1762): at dalvik.system.NativeStart.main(Native Method)