【发布时间】:2016-09-06 10:49:41
【问题描述】:
我尝试使用简单的 json 响应进行改造,该响应具有 [ {},{},{},{},{},{},] 对象数组,但当我尝试使用状态进行改造时:
{"ok",count: 4,count_total: 4,pages: 1,posts: [{},{},{},{}] }
我想出了空结果..请找到正确的解决方案我如何调用改造以获得正确的结果。
pojo 类`
public class Categories {
@SerializedName("status")
private int status;
@SerializedName("id")
private int id;
@SerializedName("type")
private String type;
@SerializedName("title")
private String title;
@SerializedName("content")
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
mainactivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<List<Categories>> call = apiService.response();
call.enqueue(new Callback<List<Categories>>() {
@Override
public void onResponse(Call<List<Categories>> call, Response<List<Categories>> response) {
List<Categories> movies = response.body();
recyclerView
.setAdapter(new MoviesAdapter(movies,
R.layout.list_item_movie, getApplicationContext()));
}
@Override
public void onFailure(Call<List<Categories>> call, Throwable t) {
}
});
}
apiclient.java
public class ApiClient {
public static final String BASE_URL = "http://androidaura.com/health/api/get_recent_posts/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
【问题讨论】:
标签: android json rest retrofit2