簡體   English   中英

從改造響應 json 數組中獲取數據

[英]Get data from retrofit response json array

我正在 android 中進行 Retrofit,我是新手。 我正確實現了 api 並獲得了 json 響應。 在我的回復中,我有狀態、數據、消息。 這里的數據類是一個數組,我發現訪問數組中的項目有困難(id、title、url、image)。 我該如何處理這些項目。

我需要將圖像 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 響應。我需要將圖像 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 類,如下所示。

社交數據.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+"]";
}

}

消息.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+"]";
}

}

數據.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+"]";
}

 }

圖像.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+"]";
}

}

我得到了很好的回應。

要為 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(context,""+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;
}

}

類 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;
}

}

希望它會幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM