Java 嵌套对象解析的改进
我有以下Java 嵌套对象解析的改进,java,android,json,retrofit,Java,Android,Json,Retrofit,我有以下JSON作为响应: 我有User类 public class User { private int id; @SerializedName("count_messages") private int countMessages; @SerializedName("count_followers") private int countFollowers; @SerializedName("count_following") private int countFollowing; @Ser
JSON
作为响应:
我有User
类
public class User {
private int id;
@SerializedName("count_messages")
private int countMessages;
@SerializedName("count_followers")
private int countFollowers;
@SerializedName("count_following")
private int countFollowing;
@SerializedName("date_created")
private String dateCreated;
@SerializedName("date_updated")
private String dateUpdated;
private String username, name, description, location, url, lang;
@SerializedName("fb_id")
private int fbID;
@SerializedName("tw_id")
private int twID;
和消息
public class Message {
private int id;
@SerializedName("date_created")
private int dateCreated;
private String title, text;
private List<String> tags;
private User user;
公共类消息{
私有int-id;
@SerializedName(“创建日期”)
创建了私有数据集;
私有字符串标题、文本;
私有列表标签;
私人用户;
我需要的是接收来自改装的列表
回调
我应该使用转换器吗?如何定义它?您好,请检查我的代码,希望它能帮助您 MainActivity.java
private final String api = "http://api.androidhive.info";
private RestAdapter adapter;
private ApiListener apis;
private void getContactList()
{
adapter = new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL).setEndpoint(api).build();
apis = adapter.create(ApiListener.class);
apis.getData(new Callback<ContactsVo>()
{
@Override
public void success(ContactsVo model, Response response)
{
progress.setVisibility(View.INVISIBLE);
}
@Override
public void failure(RetrofitError arg0)
{
progress.setVisibility(View.INVISIBLE);
}
});
}
2) 联系
public class ContactsVo
{
private ArrayList<ContactVo> contacts;
public ArrayList<ContactVo> getContacts ()
{
return contacts;
}
public void setContacts (ArrayList<ContactVo> contacts)
{
this.contacts = contacts;
}
}
公共类联系人
{
私人ArrayList联系人;
公共ArrayList getContacts()
{
返回联系人;
}
公共无效设置联系人(ArrayList联系人)
{
这个。联系人=联系人;
}
}
假设您知道如何处理改装,并具有如下界面:
public interface MyService {
@GET("/getData")
ApiResponse getData();
}
此对象将用消息表示改装响应
public class ApiResponse {
Properties properties;
public static class Properties {
List<Message> messages;
}
}
公共类响应{
属性;
公共静态类属性{
列出信息;
}
}
然后您可以通过以下方式获取信息:
ApiResponse apiResponse = myService.getData();
List<Message> messages = apiResponse.properties.messages;
ApiResponse-ApiResponse=myService.getData();
列表消息=apiResponse.properties.messages;
public class ApiResponse {
Properties properties;
public static class Properties {
List<Message> messages;
}
}
ApiResponse apiResponse = myService.getData();
List<Message> messages = apiResponse.properties.messages;