-4
我想在android中使用retrofit 2.1,但我只打印了[email protected] 7次作爲響應。我究竟做錯了什麼?安卓與改造2的故障2
的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
它打印對象的內存地址 – droidev
使用getter方法打印響應中的任何值 – droidev