简体   繁体   中英

Android Retrofit + rxjava parsing JSON

I'm new to Retrofit and RX java and I'm trying to parse the following JSON:

{
   "result": 
    {
        "offset": 0,
        "limit": 2,
        "count": 408,
        "sort": "",
        "results":[
            {
                "_id": "1",
                "iid": "338",
                "sv": "0",
                "sd": "20000101000000",
                "vtyp": "1",
                "sno": "0001",            
            },
            {
                "_id": "2",
                "iid": "339",
                "sv": "0",
                "sd": "20000101000000",
                "vtyp": "1",
                "sno": "0001",          
            }

        ]
    }

}

I'm only interested in the array of objects contained in "results". From what I've been able to find I should be implementing a JsonDeserializer to get at that data and use .setConverter when building the RestAdapter. Here's what I have now:

class MyDeserializer implements JsonDeserializer<Pojo> {
    @Override
    public Station deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
        JsonElement element = json.getAsJsonObject().get("results");
        return new Gson().fromJson(element, Pojo.class);
    }

I want the retrofit GET call to return

Observable<List<Pojo>>

that represents the objects in "results"

my Pojo.class looks like this:

public class Pojo {
    public String _id;
    public String iid;
    public String sv;
    public String sd;
    public String vtyp;
}

Write now I get this error when I try to call the API, so I think I've got something wrong in the Json deserialization:

retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $

Any help would be appreciated!

You need to create below three pojo classes

  1. MyPojo.java

     public class MyPojo { private Result result; public Result getResult () { return result; } public void setResult (Result result) { this.result = result; } } 
  2. Result.java

     public class Result { private String limit; private String sort; private Results[] results; private String count; private String offset; public String getLimit () { return limit; } public void setLimit (String limit) { this.limit = limit; } public String getSort () { return sort; } public void setSort (String sort) { this.sort = sort; } public Results[] getResults () { return results; } public void setResults (Results[] results) { this.results = results; } public String getCount () { return count; } public void setCount (String count) { this.count = count; } public String getOffset () { return offset; } public void setOffset (String offset) { this.offset = offset; } 

    }

  3. Results.java

     public class Results { private String sno; private String _id; private String iid; private String vtyp; private String sd; private String sv; public String getSno () { return sno; } public void setSno (String sno) { this.sno = sno; } public String get_id () { return _id; } public void set_id (String _id) { this._id = _id; } public String getIid () { return iid; } public void setIid (String iid) { this.iid = iid; } public String getVtyp () { return vtyp; } public void setVtyp (String vtyp) { this.vtyp = vtyp; } public String getSd () { return sd; } public void setSd (String sd) { this.sd = sd; } public String getSv () { return sv; } public void setSv (String sv) { this.sv = sv; } 

    }

write below code in your activity

private RestAdapter restAdapter;

private ApiListeners apiListener;

restAdapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint("Your Base Url").build();
apiListener = restAdapter.create(ApiListeners.class);

apiListener.getData(new Callback<MyPojo>() 
        {
            @Override
            public void success(MyPojo pojo, Response response)
            {

            }

            @Override
            public void failure(RetrofitError error) 
            {

            }
        });

Create below listener

public interface ApiListeners 
{
    @GET("/yourApiTag")
    public void getData(retrofit.Callback<MyPojo> responseVo);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM