【发布时间】:2019-01-22 16:06:26
【问题描述】:
有人可以帮助我在 android 中使用改造来接收这个对象吗?我希望这些对象作为数组列表。
{"UtilDataType":[{"name":"Pondicherry","id":22},{"name":"Vizianagaram","id":23},{"name":"Srikakulam","id":24}]}
我的 API 接口
public interface MyAPI {
@GET("city")
Call<ResponseBody> getCities(@Query("first") long since, @Query("max") int perPage);
}
我的模型课
public class City {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
public static DiffUtil.ItemCallback<City> DIFF_CALLBACK = new DiffUtil.ItemCallback<City>() {
@Override
public boolean areItemsTheSame(@NonNull City oldItem, @NonNull City newItem) {
return oldItem.id == newItem.id;
}
@Override
public boolean areContentsTheSame(@NonNull City oldItem, @NonNull City newItem) {
return oldItem.equals(newItem);
}
};
}
我尝试了很多方法,但仍在尝试。下面是方法。
service.getCities(first,max).enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.i("blb07",""+response.body().toString());
try {
UtilClass utilClass=new Gson().fromJson(response.body().toString(),UtilClass.class);
//Log.i("blb07",""+utilClass.getUtilDataType());
}catch (Exception e){
Log.i("blb07","hello "+e.getMessage());
}
//JSONArray userArray = response.getJSONArray("UtilDataType");
//ArrayList<City> temp=new Gson().fromJson(response.body().toString(), new TypeToken<List<City>>() {}.getType());
//Log.i("blb07",""+temp.size());
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.i("blb07",""+t.getMessage());
}
});
我的目标是将这些城市对象放在一个 arrylist 中,用于 android 中的分页概念。现在我被困在这件事上。请有人帮帮我。
【问题讨论】:
标签: android json retrofit retrofit2 gson