[英]Android Retrofit + rxjava parsing JSON
I'm new to Retrofit and RX java and I'm trying to parse the following JSON: 我是Retrofit和RX Java的新手,我正在尝试解析以下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. 根据我的发现,我应该实现一个JsonDeserializer来获取该数据,并在构建RestAdapter时使用.setConverter。 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 我希望翻新的GET调用返回
Observable<List<Pojo>>
that represents the objects in "results" 代表“结果”中的对象
my Pojo.class looks like this: 我的Pojo.class看起来像这样:
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: 现在写,当我尝试调用API时遇到此错误,因此我认为Json反序列化中出现了问题:
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 您需要在以下三个pojo类中创建
MyPojo.java MyPojo.java
public class MyPojo { private Result result; public Result getResult () { return result; } public void setResult (Result result) { this.result = result; } }
Result.java 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; }
} }
Results.java 结果.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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.