2015-08-28 2 views
1

У меня есть следующие JSON в ответ:Ретрофит разбор вложенного объекта

http://pastebin.com/Qir7eNsP

У меня есть класс 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> от модернизации.

Должен ли я использовать конвертер и как его определить?

ответ

1

Предполагая, что вы знаете, как бороться с Модернизированным и иметь некоторый интерфейс, как это:

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; 
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; 
     } 

}