嵌套对象的改造解析

Retrofit parsing of nested Object

我有以下 JSON 作为回复:

http://pastebin.com/Qir7eNsP

我有 User class

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> 回调。

我应该使用转换器吗?我该如何定义它?

您好,请检查我的代码,希望对您有所帮助

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 个调用创建一个 class
ApiListener.java

public interface ApiListener
{
    @GET("/contacts") 
    public void getData(retrofit.Callback<ContactsVo> response);
}

您需要根据需要创建相应的 pojo class,

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) 联系人数量

public class ContactsVo
{
     private ArrayList<ContactVo> contacts;

        public ArrayList<ContactVo> getContacts ()
        {
            return contacts;
        }

        public void setContacts (ArrayList<ContactVo> contacts)
        {
            this.contacts = contacts;
        }

}

假设您知道如何处理 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;