2017-02-14 7 views
-1

私はアンドロイドでRetrofitに取り組んでいます。これは初めてのことです。私はapiを正しく実装し、jsonの応答を得ました。 私の応答で私はステータス、データ、メッセージを持っています。ここでデータクラスは配列であり、配列内の項目(id、title、url、image)にアクセスするのが難しいと思っています。どうすればこれらのアイテムを扱うことができますか?改造からのデータ取得json array

イメージURLをイメージビューに設定する必要があります。

これは私が配列の数を見ることができていますので、ここで改造

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) { 

       } 
      }); 

を呼び出して、私のJavaクラスです。

これは私のjsonレスポンスです。image 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+"]"; 
} 

} 

私は応答がうまくいきます。 http://www.jsonschema2pojo.org

とoyur JSON

セットソースタイプコピー:あなたは正しいクラスを取得しますJSON セット注釈スタイル:: GSON

をレトロフィットは、このサイトを使用するための正しいPOJOクラスを作成するには

答えて

0

。 。

データに

を取得します

SocialData data = response.body(); 

等SocialDataのオブジェクトを作成し、データオブジェクト を使用します。

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

Toast.makeText(コンテキスト "" + 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; 
} 

} 

クラスあなたが明示的に `excludeFieldsWithoutExposeAnnotation()`とGsonを初期化しない限り、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

あなたを助けることを願っていますFYI 'Expose'アノテーションは無意味です。また、変数名とjsonキーが同じであるため、 'SerializedName'アノテーションは必要ありません。変数** **はそのように宣言される必要はありません – akash93

+0

私には新しい情報..ありがとう。最新の回答@ akash93 –

+0

データムクラスの互換性のない型が表示されています。 –

関連する問題