我有以下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;
和Message
public class Message {
private int id;
@SerializedName("date_created")
private int dateCreated;
private String title, text;
private List<String> tags;
private User user;
我需要的是从改造中收到List<Message>
回调。
我应该使用转换器吗?如何定义它?
答案 0 :(得分:1)
您好,请检查我的代码,希望它能为您提供帮助
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);
}
});
}
您需要为所有api调用创建一个类
ApiListener.java
public interface ApiListener
{
@GET("/contacts")
public void getData(retrofit.Callback<ContactsVo> response);
}
您需要根据需要创建相应的pojo类,
ContactVo.java
public class ContactVo
{
private String id;
private String address;
private String email;
private String name;
private String gender;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getGender()
{
return gender;
}
public void setGender(String gender)
{
this.gender = gender;
}
}
2)ContactsVo
public class ContactsVo
{
private ArrayList<ContactVo> contacts;
public ArrayList<ContactVo> getContacts ()
{
return contacts;
}
public void setContacts (ArrayList<ContactVo> contacts)
{
this.contacts = contacts;
}
}
答案 1 :(得分:1)
假设您知道如何处理Retrofit并拥有如下界面:
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;