2017-02-14 55 views
-1

我正在開發Android版Retrofit,我對此很陌生。我正確實現了api並獲得了json響應。 在我的回覆中,我有狀態,數據,信息。這裏的數據類是一個數組,我發現訪問數組內的項目(id,title,url,image)時遇到困難。我如何工作這些項目。從改造響應中獲取數據json array

我需要將圖像url設置爲imageview。

這是我的java類在那裏我打電話改造

ApiInterface apiInterface = AppController.GetRetrofitObject().create(ApiInterface.class); 
      Call<SocialData> call = apiInterface.socialContent(accessToken,tokenType,client,expiry,uid); 
      call.enqueue(new Callback<SocialData>() { 
       @Override 
       public void onResponse(Call<SocialData> call, Response<SocialData> response) { 

        Data[] data=response.body().getData(); 
        i=data.length; 
        String count= String.valueOf(i); 
        Toast.makeText(context,count,Toast.LENGTH_LONG).show(); 

       } 

       @Override 
       public void onFailure(Call<SocialData> call, Throwable t) { 

       } 
      }); 

所以在這裏我能看到陣列計數。

這是我的json response.I需要設置圖像url到imageview。

{ 
    "status": 200, 
    "data": [ 
    { 
    "id": 1, 
    "title": "Six WhatsApp Features You May Not Know About ", 
    "url": "http://gadgets.ndtv.com/apps/features/six-whatsapp-features-you-may-not-know-about-1658812?pfrom=home-indepth", 
    "image": { 
    "url": "/uploads/social_medium/image/1/Whatsapp-for-PC.jpg" 
    }, 
    "bypass": false 
}, 
{ 
    "id": 2, 
    "title": "How to Delete Your Snapchat Account ", 
    "url": "http://gadgets.ndtv.com/apps/features/how-to-delete-your-snapchat-account-1658799?pfrom=home-indepth", 
    "image": { 
    "url": "/uploads/social_medium/image/2/snapchat_code_picjumbo_1486964243543.jpg" 
    }, 
    "bypass": false 
}, 
{ 
    "id": 3, 
    "title": "Jadeja, Ishant wrap up India's 208-run win", 
    "url": "http://www.espncricinfo.com/india-v-bangladesh-2016-17/content/story/1082146.html", 
    "image": { 
    "url": "/uploads/social_medium/image/3/259024.jpg" 
    }, 
    "bypass": false 
}, 
{ 
    "id": 4, 
    "title": "10 Facts On the Disproportionate Case Against VK Sasikala", 
    "url": "http://www.ndtv.com/india-news/10-facts-on-the-disproportionate-case-against-vk-sasikala-1659078", 
    "image": { 
    "url": null 
    }, 
    "bypass": false 
} 

], 
    "message": { 
"success": "Success" 
    } 
    } 

我得到pojo類,如下所示。

SocialData.java

public class SocialData { 

private Message message; 

private String status; 

private Data[] data; 

public Message getMessage() 
{ 
    return message; 
} 

public void setMessage (Message message) 
{ 
    this.message = message; 
} 

public String getStatus() 
{ 
    return status; 
} 

public void setStatus (String status) 
{ 
    this.status = status; 
} 

public Data[] getData() 
{ 
    return data; 
} 

public void setData (Data[] data) 
{ 
    this.data = data; 
} 

@Override 
public String toString() 
{ 
    return "ClassPojo [message = "+message+", status = "+status+", data = "+data+"]"; 
} 

}

Message.java

public class Message { 

private String success; 

public String getSuccess() 
{ 
    return success; 
} 

public void setSuccess (String success) 
{ 
    this.success = success; 
} 

@Override 
public String toString() 
{ 
    return "ClassPojo [success = "+success+"]"; 
} 

} 

Data.java

public class Data { 

private String id; 
private String title; 
private String bypass; 
private Image image; 

private String url; 

public String getId() 
{ 
    return id; 
} 

public void setId (String id) 
{ 
    this.id = id; 
} 

public String getTitle() 
{ 
    return title; 
} 

public void setTitle (String title) 
{ 
    this.title = title; 
} 

public String getBypass() 
{ 
    return bypass; 
} 

public void setBypass (String bypass) 
{ 
    this.bypass = bypass; 
} 

public Image getImage() 
{ 
    return image; 
} 

public void setImage (Image image) 
{ 
    this.image = image; 
} 

public String getUrl() 
{ 
    return url; 
} 

public void setUrl (String url) 
{ 
    this.url = url; 
} 

@Override 
public String toString() 
{ 
    return "ClassPojo [id = "+id+", title = "+title+", bypass = "+bypass+", image = "+image+", url = "+url+"]"; 
} 

} 

Image.java

public class Image { 

private String url; 

public String getUrl() 
{ 
    return url; 
} 

public void setUrl (String url) 
{ 
    this.url = url; 
} 

@Override 
public String toString() 
{ 
    return "ClassPojo [url = "+url+"]"; 
} 

} 

我得到的回覆很好。

回答

0

要建立正確的POJO類改造使用這個網站:http://www.jsonschema2pojo.org

和複製oyur JSON

組源類型:JSON 組註釋風格:: GSON

你會得到正確的類。 。

獲取數據

創建SocialData的目的等

SocialData data = response.body(); 

,並使用該數據對象 。 像

List<Datum> datalist = new ArrayList<>(); 
datalist = data.getData(); 

Toast.makeText(上下文 「」 + datalist.size(),Toast.LENGTH_LONG)。顯示();

更新

類基準

package com.example; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Datum { 

@SerializedName("id") 
@Expose 
private Integer id; 
@SerializedName("title") 
@Expose 
private String title; 
@SerializedName("url") 
@Expose 
private String url; 
@SerializedName("image") 
@Expose 
private Image image; 
@SerializedName("bypass") 
@Expose 
private Boolean bypass; 

/** 
* No args constructor for use in serialization 
* 
*/ 
public Datum() { 
} 

/** 
* 
* @param id 
* @param title 
* @param bypass 
* @param image 
* @param url 
*/ 
public Datum(Integer id, String title, String url, Image image, Boolean bypass) { 
super(); 
this.id = id; 
this.title = title; 
this.url = url; 
this.image = image; 
this.bypass = bypass; 
} 

public Integer getId() { 
return id; 
} 

public void setId(Integer id) { 
this.id = id; 
} 

public String getTitle() { 
return title; 
} 

public void setTitle(String title) { 
this.title = title; 
} 

public String getUrl() { 
return url; 
} 

public void setUrl(String url) { 
this.url = url; 
} 

public Image getImage() { 
return image; 
} 

public void setImage(Image image) { 
this.image = image; 
} 

public Boolean getBypass() { 
return bypass; 
} 

public void setBypass(Boolean bypass) { 
this.bypass = bypass; 
} 

} 

類圖片

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Image { 

@SerializedName("url") 
@Expose 
private Object url; 

/** 
* No args constructor for use in serialization 
* 
*/ 
public Image() { 
} 

/** 
* 
* @param url 
*/ 
public Image(Object url) { 
super(); 
this.url = url; 
} 

public Object getUrl() { 
return url; 
} 

public void setUrl(Object url) { 
this.url = url; 
} 

} 

類SocialData

import java.util.List; 
import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class SocialData { 

@SerializedName("status") 
@Expose 
private Integer status; 
@SerializedName("data") 
@Expose 
private List<Datum> data = null; 
@SerializedName("message") 
@Expose 
private Message message; 

/** 
* No args constructor for use in serialization 
* 
*/ 
public SocialData() { 
} 

/** 
* 
* @param message 
* @param status 
* @param data 
*/ 
public SocialData(Integer status, List<Datum> data, Message message) { 
super(); 
this.status = status; 
this.data = data; 
this.message = message; 
} 

public Integer getStatus() { 
return status; 
} 

public void setStatus(Integer status) { 
this.status = status; 
} 

public List<Datum> getData() { 
return data; 
} 

public void setData(List<Datum> data) { 
this.data = data; 
} 

public Message getMessage() { 
return message; 
} 

public void setMessage(Message message) { 
this.message = message; 
} 

} 

希望它會幫助你

+0

僅供參考'Expose'註釋是沒有意義的,除非你明確初始化GSON用'excludeFieldsWithoutExposeAnnotation()'。另外'SerializedName'註釋是不需要的,因爲變量名和json鍵是一樣的。所以變量**不需要**以這種方式聲明 – akash93

+0

哇..新的信息給我..謝謝..更新我的回答@ akash93 –

+0

它顯示數據類的不兼容類型。 –