我正在研究Android中的Retrofit,我是新手。我正确实现了api并获得了json响应。 在我的回复中,我有状态,数据,消息。这里的数据类是一个数组,我发现很难访问数组中的项(id,title,url,image)。我该如何处理这些物品。
我需要将图片网址设置为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回复。我需要将图片网址设置为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 :(得分:0)
要为Retrofit创建正确的pojo类,请使用此站点:http://www.jsonschema2pojo.org
并复制oyur json
设置源类型:JSON 设置注释样式:: GSON
你会得到正确的课程..
获取数据
创建一个SocialData对象,如
SocialData data = response.body();
并使用数据对象 。 喜欢
List<Datum> datalist = new ArrayList<>();
datalist = data.getData();
Toast.makeText(上下文中,#34;&#34 + datalist.size(),Toast.LENGTH_LONG).show();
更新
班级基准
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;
}
}
Class 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;
}
}
希望它会帮助你