[英]Get data from retrofit response json array
I am working on Retrofit in android and i am new to this.我正在 android 中进行 Retrofit,我是新手。 I implemented the api correctly and getting the json response.
我正确实现了 api 并获得了 json 响应。 In my response i have status, data, message.
在我的回复中,我有状态、数据、消息。 Here data class is an array, and i am finding difficulty in accessing the items inside the array(id,title,url,image).
这里的数据类是一个数组,我发现访问数组中的项目有困难(id、title、url、image)。 how can i work these items.
我该如何处理这些项目。
I need to set image url to imageview.我需要将图像 url 设置为 imageview。
This is my java class where i call retrofit这是我称之为改造的 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) {
}
});
so here i am able to see the array count.所以在这里我可以看到数组计数。
This is my json response.I need to set image url to imageview.这是我的 json 响应。我需要将图像 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"
}
}
I obtained pojo classes and are as follows.我获得了 pojo 类,如下所示。
SocialData.java社交数据.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消息.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数据.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图像.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+"]";
}
}
I am getting response fine.我得到了很好的回应。
To create correct pojo classes for Retrofit use this site: http://www.jsonschema2pojo.org要为 Retrofit 创建正确的 pojo 类,请使用此站点: http : //www.jsonschema2pojo.org
and copy oyur json并复制 oyur json
set source type : JSON set Annotation style:: GSON设置源类型:JSON 设置注释样式:: GSON
you will get correct classes..你会得到正确的课程..
to get the data
获取数据
create an object of SocialData like创建一个 SocialData 对象,如
SocialData data = response.body();
and use the data object .并使用数据对象。 like
喜欢
List<Datum> datalist = new ArrayList<>();
datalist = data.getData();
Toast.makeText(context,""+datalist.size(),Toast.LENGTH_LONG).show(); Toast.makeText(context,""+datalist.size(),Toast.LENGTH_LONG).show();
Update
更新
Class Datum
类数据
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;
}
}
Class Image
班级形象
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
类 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;
}
}
hope it will help you希望它会帮助你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.