我正在尝试在android中使用retrofit 2.1但是我只打印了7次作为响应的com.example.netapp.Model.PostUpdates@87943a3。我做错了什么?
的Json
[{
"id": "2",
"categories_id": "2",
"title": "समाज",
"featured_news": "0",
"cover_img": "50D5C03F4C476E718D615DEB3240CCB275E027A2.jpg",
"meta_desc": "एक चीन नीतिमा प्रतिवद्ध छौं: अर्थमन्त्री महरा",
"details": "उपप्रधान तथा अर्थमन्त्री कृष्णबहादुर महरासँग नेपालका लागि चीनका नवनियुक्त राजदुत यु होङले आज अर्थमन्त्रालयमा भेटबार्ता गरेकीछन्।",
"status": "1",
"postdate": "2016-11-18 16:45:16"
},
...]
主要活动类:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
ApiService api_service = ApiService.retrofit.create(ApiService.class);
final Call<List<PostUpdates>> call = api_service.getUpdates("updates");
call.enqueue(new Callback<List<PostUpdates>>() {
@Override
public void onResponse(Call<List<PostUpdates>> call, Response<List<PostUpdates>> response) {
textView.setText(response.body().toString());
}
@Override
public void onFailure(Call<List<PostUpdates>> call, Throwable t) {
textView.setText(t.getMessage().toString());
}
});
}
ApiService接口
public interface ApiService{
String ENDPOINT = "http://localhost/test_api/";
@GET("api/{param}")
Call<List<PostUpdates>> getUpdates(@Path("param") String param);
public final static Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
PostUpdates
public class PostUpdates {
private String id;
private String categoriesId;
private String title;
private String isFeatured;
private String featuredImg;
private String metaDesc;
private String details;
private String status;
private String postdate;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
*
* @return
* The id
*/
public String getId() {
return id;
}
/**
*
* @param id
* The id
*/
public void setId(String id) {
this.id = id;
}
/**
*
* @return
* The categoriesId
*/
public String getCategoriesId() {
return categoriesId;
}
/**
*
* @param categoriesId
* The categories_id
*/
public void setCategoriesId(String categoriesId) {
this.categoriesId = categoriesId;
}
/**
*
* @return
* The title
*/
public String getTitle() {
return title;
}
/**
*
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}
/**
*
* @return
* The isFeatured
*/
public String getIsFeatured() {
return isFeatured;
}
/**
*
* @param isFeatured
* The is_featured
*/
public void setIsFeatured(String isFeatured) {
this.isFeatured = isFeatured;
}
/**
*
* @return
* The featuredImg
*/
public String getFeaturedImg() {
return featuredImg;
}
/**
*
* @param featuredImg
* The featured_img
*/
public void setFeaturedImg(String featuredImg) {
this.featuredImg = featuredImg;
}
/**
*
* @return
* The metaDesc
*/
public String getMetaDesc() {
return metaDesc;
}
/**
*
* @param metaDesc
* The meta_desc
*/
public void setMetaDesc(String metaDesc) {
this.metaDesc = metaDesc;
}
/**
*
* @return
* The details
*/
public String getDetails() {
return details;
}
/**
*
* @param details
* The details
*/
public void setDetails(String details) {
this.details = details;
}
/**
*
* @return
* The status
*/
public String getStatus() {
return status;
}
/**
*
* @param status
* The status
*/
public void setStatus(String status) {
this.status = status;
}
/**
*
* @return
* The postdate
*/
public String getPostdate() {
return postdate;
}
/**
*
* @param postdate
* The postdate
*/
public void setPostdate(String postdate) {
this.postdate = postdate;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
Android应用程序显示此结果 screenshot here
答案 0 :(得分:0)
哦!我得到它需要在响应体对象中使用getter方法来获取相应的数据。谢谢droidev
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
ApiService api_service = ApiService.retrofit.create(ApiService.class);
final Call<List<PostUpdates>> call = api_service.getUpdates("updates");
call.enqueue(new Callback<List<PostUpdates>>() {
@Override
public void onResponse(Call<List<PostUpdates>> call, Response<List<PostUpdates>> response) {
StringBuffer sb = new StringBuffer();
for(PostUpdates field : response.body()) {
sb.append( field.getTitle() + "\n");
}
Log.d(TAG, "onResponse: " + sb.toString());
textView.setText(response.body().toString());
}
@Override
public void onFailure(Call<List<PostUpdates>> call, Throwable t) {
textView.setText(t.getMessage().toString());
}
});
}