0

私はいくつかのデータの配列を含むjsonオブジェクトをシリアル化するためにGson解析を使用しているアプリケーションを開発しています..しかしここで私が解析しているメソッドにいくつかの問題があります...それは私を正常に解析しません..私は解析を試みたことがありませんSerializeアノテーション付きの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;

 }

これがAnnotaionベースの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)
4

1 に答える 1

1

You are not considering the fact that there's a Json Array that need to be serialized in your pojo

The following code should work

public class GetAllAssetsResult
{
    @SerializedName("GetAllAssetsResult")
    public List<Asset> assets;

    class Asset
    {
        @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;
    }
}
于 2012-11-16T14:05:48.730 に答える