2016-06-22 12 views
1

Retrofitを使用してオブジェクトのリストを取得しようとしましたが、応答ボディが半分空であり、リストにnullフィールドが含まれています。サーバーは適切な応答を送信していますが、アプリケーションでこれらの値を取得できません。どのようにそれを解決するには?私のRESTサービスクラスの改造体は空のフィールドを持つ半オブジェクトを受け取りました

パート:私はRESTクエリを取得しよう

Retrofit retrofit = new Retrofit.Builder() 
    .baseUrl("https://openapi.etsy.com/v2/") 
    .addConverterFactory(GsonConverterFactory.create()) 
    .build(); 

@GET("listings/{listingId}/images") 
    Call<ListingImages> getImages(@Path("listingId") String listingId, 
            @Query("api_key") String apiKey); 

RESTサーバー応答

{ 
    "count": 5, 
    "results": [ 
    { 
     "listing_image_id": 207779028, 
     "hex_code": "D9CEBF", 
     "red": 217, 
     "green": 206, 
     "blue": 191, 
     "hue": 34, 
     "saturation": 11, 
     "brightness": 85, 
     "is_black_and_white": false, 
     "creation_tsz": 1299877281, 
     "listing_id": 65811540, 
     "rank": 1, 
     "url_75x75": "https://img0.etsystatic.com/000/0/5499432/il_75x75.207779028.jpg", 
     "url_170x135": "https://img0.etsystatic.com/000/0/5499432/il_170x135.207779028.jpg", 
     "url_570xN": "https://img0.etsystatic.com/000/0/5499432/il_570xN.207779028.jpg", 
     "url_fullxfull": "https://img0.etsystatic.com/000/0/5499432/il_fullxfull.207779028.jpg", 
     "full_height": 1500, 
     "full_width": 1125 
    }, 
    { 
     "listing_image_id": 207778696, 
     "hex_code": "BFC0AA", 
     "red": 191, 
     "green": 192, 
     "blue": 170, 
     "hue": 62, 
     "saturation": 11, 
     "brightness": 75, 
     "is_black_and_white": false, 
     "creation_tsz": 1299877281, 
     "listing_id": 65811540, 
     "rank": 2, 
     "url_75x75": "https://img0.etsystatic.com/000/0/5499432/il_75x75.207778696.jpg", 
     "url_170x135": "https://img0.etsystatic.com/000/0/5499432/il_170x135.207778696.jpg", 
     "url_570xN": "https://img0.etsystatic.com/000/0/5499432/il_570xN.207778696.jpg", 
     "url_fullxfull": "https://img0.etsystatic.com/000/0/5499432/il_fullxfull.207778696.jpg", 
     "full_height": 1125, 
     "full_width": 1500 
    }, 
    { 
     "listing_image_id": 207778607, 
     "hex_code": "BBBBA6", 
     "red": 187, 
     "green": 187, 
     "blue": 166, 
     "hue": 60, 
     "saturation": 11, 
     "brightness": 73, 
     "is_black_and_white": false, 
     "creation_tsz": 1299877281, 
     "listing_id": 65811540, 
     "rank": 3, 
     "url_75x75": "https://img1.etsystatic.com/000/0/5499432/il_75x75.207778607.jpg", 
     "url_170x135": "https://img1.etsystatic.com/000/0/5499432/il_170x135.207778607.jpg", 
     "url_570xN": "https://img1.etsystatic.com/000/0/5499432/il_570xN.207778607.jpg", 
     "url_fullxfull": "https://img1.etsystatic.com/000/0/5499432/il_fullxfull.207778607.jpg", 
     "full_height": 1125, 
     "full_width": 1500 
    }, 
    { 
     "listing_image_id": 207778809, 
     "hex_code": "BDBCA9", 
     "red": 189, 
     "green": 188, 
     "blue": 169, 
     "hue": 57, 
     "saturation": 10, 
     "brightness": 74, 
     "is_black_and_white": false, 
     "creation_tsz": 1299877281, 
     "listing_id": 65811540, 
     "rank": 4, 
     "url_75x75": "https://img1.etsystatic.com/000/0/5499432/il_75x75.207778809.jpg", 
     "url_170x135": "https://img1.etsystatic.com/000/0/5499432/il_170x135.207778809.jpg", 
     "url_570xN": "https://img1.etsystatic.com/000/0/5499432/il_570xN.207778809.jpg", 
     "url_fullxfull": "https://img1.etsystatic.com/000/0/5499432/il_fullxfull.207778809.jpg", 
     "full_height": 1500, 
     "full_width": 1125 
    }, 
    { 
     "listing_image_id": 207778913, 
     "hex_code": "E2D7C6", 
     "red": 226, 
     "green": 215, 
     "blue": 198, 
     "hue": 36, 
     "saturation": 12, 
     "brightness": 88, 
     "is_black_and_white": false, 
     "creation_tsz": 1299877281, 
     "listing_id": 65811540, 
     "rank": 5, 
     "url_75x75": "https://img1.etsystatic.com/000/0/5499432/il_75x75.207778913.jpg", 
     "url_170x135": "https://img1.etsystatic.com/000/0/5499432/il_170x135.207778913.jpg", 
     "url_570xN": "https://img1.etsystatic.com/000/0/5499432/il_570xN.207778913.jpg", 
     "url_fullxfull": "https://img1.etsystatic.com/000/0/5499432/il_fullxfull.207778913.jpg", 
     "full_height": 1500, 
     "full_width": 1125 
    } 
    ], 
    "params": { 
    "listing_id": "65811540" 
    }, 
    "type": "ListingImage", 
    "pagination": {} 
} 

クラス:

public class GetImages { 
    EtsyService etsyService; 
    final private String API_KEY = "00000000000000000000";//fake key 

    public List<ListingEntity> fillModel(List<ListingEntity> listingList){ 

     etsyService = EtsyService.retrofit.create(EtsyService.class); 

     for(final ListingEntity listingentity:listingList){ 

      final Call<ListingImages> call = etsyService.getImages(listingentity.getListing_id(),API_KEY); 
      call.enqueue(new Callback<ListingImages>() { 
       @Override 
       public void onResponse(Call<ListingImages> call, Response<ListingImages> response) { 
        listingentity.setListingImagesUrl(response.body().getResults()); 
       } 

       @Override 
       public void onFailure(Call<ListingImages> call, Throwable t) { 
        Log.e(MainActivity.class.getName(),call.toString()); 
       } 
      }); 
     } 
     return listingList; 
    } 
} 

私からの応答を受け取りますすべてのフィールドではなく、すべてのフィールドが満たされています。以前のクラスからの結果の

Response body in debugger

(gson2pojoサービスを使用して生成された)応答のモデル

public class ListingImages { 

    private Integer count; 
    private List<Result> results = new ArrayList<Result>(); 
    private Params params; 
    private String type; 
    private Pagination pagination; 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 
    public ListingImages() { 
    } 

    /** 
    * 
    * @param results 
    * @param count 
    * @param params 
    * @param pagination 
    * @param type 
    */ 
    public ListingImages(Integer count, List<Result> results, Params params, String type, Pagination pagination) { 
     this.count = count; 
     this.results = results; 
     this.params = params; 
     this.type = type; 
     this.pagination = pagination; 
    } 

    /** 
    * 
    * @return 
    *  The count 
    */ 
    public Integer getCount() { 
     return count; 
    } 

    /** 
    * 
    * @param count 
    *  The count 
    */ 
    public void setCount(Integer count) { 
     this.count = count; 
    } 

    /** 
    * 
    * @return 
    *  The results 
    */ 
    public List<Result> getResults() { 
     return results; 
    } 

    /** 
    * 
    * @param results 
    *  The results 
    */ 
    public void setResults(List<Result> results) { 
     this.results = results; 
    } 

    /** 
    * 
    * @return 
    *  The params 
    */ 
    public Params getParams() { 
     return params; 
    } 

    /** 
    * 
    * @param params 
    *  The params 
    */ 
    public void setParams(Params params) { 
     this.params = params; 
    } 

    /** 
    * 
    * @return 
    *  The type 
    */ 
    public String getType() { 
     return type; 
    } 

    /** 
    * 
    * @param type 
    *  The type 
    */ 
    public void setType(String type) { 
     this.type = type; 
    } 

    /** 
    * 
    * @return 
    *  The pagination 
    */ 
    public Pagination getPagination() { 
     return pagination; 
    } 

    /** 
    * 
    * @param pagination 
    *  The pagination 
    */ 
    public void setPagination(Pagination pagination) { 
     this.pagination = pagination; 
    } 

    public Map<String, Object> getAdditionalProperties() { 
     return this.additionalProperties; 
    } 

    public void setAdditionalProperty(String name, Object value) { 
     this.additionalProperties.put(name, value); 
    } 

} 

クラス(gson2pojoサービスに発生する)

public class Result { 

    private Integer listingImageId; 
    private String hexCode; 
    private String red; 
    private int green; 
    private Integer blue; 
    private Integer hue; 
    private Integer saturation; 
    private Integer brightness; 
    private Boolean isBlackAndWhite; 
    private Integer creationTsz; 
    private Integer listingId; 
    private Integer rank; 
    private String url75x75; 
    private String url170x135; 
    private String url570xN; 
    private String urlFullxfull; 
    private Integer fullHeight; 
    private Integer fullWidth; 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

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

    /** 
    * 
    * @param url570xN 
    * @param urlFullxfull 
    * @param url170x135 
    * @param green 
    * @param hue 
    * @param fullWidth 
    * @param fullHeight 
    * @param listingId 
    * @param isBlackAndWhite 
    * @param brightness 
    * @param rank 
    * @param red 
    * @param blue 
    * @param saturation 
    * @param creationTsz 
    * @param hexCode 
    * @param url75x75 
    * @param listingImageId 
    */ 
    public Result(Integer listingImageId, String hexCode, String red, int green, Integer blue, Integer hue, Integer saturation, Integer brightness, Boolean isBlackAndWhite, Integer creationTsz, Integer listingId, Integer rank, String url75x75, String url170x135, String url570xN, String urlFullxfull, Integer fullHeight, Integer fullWidth) { 
     this.listingImageId = listingImageId; 
     this.hexCode = hexCode; 
     this.red = red; 
     this.green = green; 
     this.blue = blue; 
     this.hue = hue; 
     this.saturation = saturation; 
     this.brightness = brightness; 
     this.isBlackAndWhite = isBlackAndWhite; 
     this.creationTsz = creationTsz; 
     this.listingId = listingId; 
     this.rank = rank; 
     this.url75x75 = url75x75; 
     this.url170x135 = url170x135; 
     this.url570xN = url570xN; 
     this.urlFullxfull = urlFullxfull; 
     this.fullHeight = fullHeight; 
     this.fullWidth = fullWidth; 
    } 

    /** 
    * 
    * @return 
    *  The listingImageId 
    */ 
    public Integer getListingImageId() { 
     return listingImageId; 
    } 

    /** 
    * 
    * @param listingImageId 
    *  The listing_image_id 
    */ 
    public void setListingImageId(Integer listingImageId) { 
     this.listingImageId = listingImageId; 
    } 

    /** 
    * 
    * @return 
    *  The hexCode 
    */ 
    public String getHexCode() { 
     return hexCode; 
    } 

    /** 
    * 
    * @param hexCode 
    *  The hex_code 
    */ 
    public void setHexCode(String hexCode) { 
     this.hexCode = hexCode; 
    } 

    /** 
    * 
    * @return 
    *  The red 
    */ 
    public String getRed() { 
     return red; 
    } 

    /** 
    * 
    * @param red 
    *  The red 
    */ 
    public void setRed(String red) { 
     this.red = red; 
    } 

    /** 
    * 
    * @return 
    *  The green 
    */ 
    public int getGreen() { 
     return green; 
    } 

    /** 
    * 
    * @param green 
    *  The green 
    */ 
    public void setGreen(int green) { 
     this.green = green; 
    } 

    /** 
    * 
    * @return 
    *  The blue 
    */ 
    public Integer getBlue() { 
     return blue; 
    } 

    /** 
    * 
    * @param blue 
    *  The blue 
    */ 
    public void setBlue(Integer blue) { 
     this.blue = blue; 
    } 

    /** 
    * 
    * @return 
    *  The hue 
    */ 
    public Integer getHue() { 
     return hue; 
    } 

    /** 
    * 
    * @param hue 
    *  The hue 
    */ 
    public void setHue(Integer hue) { 
     this.hue = hue; 
    } 

    /** 
    * 
    * @return 
    *  The saturation 
    */ 
    public Integer getSaturation() { 
     return saturation; 
    } 

    /** 
    * 
    * @param saturation 
    *  The saturation 
    */ 
    public void setSaturation(Integer saturation) { 
     this.saturation = saturation; 
    } 

    /** 
    * 
    * @return 
    *  The brightness 
    */ 
    public Integer getBrightness() { 
     return brightness; 
    } 

    /** 
    * 
    * @param brightness 
    *  The brightness 
    */ 
    public void setBrightness(Integer brightness) { 
     this.brightness = brightness; 
    } 

    /** 
    * 
    * @return 
    *  The isBlackAndWhite 
    */ 
    public Boolean getIsBlackAndWhite() { 
     return isBlackAndWhite; 
    } 

    /** 
    * 
    * @param isBlackAndWhite 
    *  The is_black_and_white 
    */ 
    public void setIsBlackAndWhite(Boolean isBlackAndWhite) { 
     this.isBlackAndWhite = isBlackAndWhite; 
    } 

    /** 
    * 
    * @return 
    *  The creationTsz 
    */ 
    public Integer getCreationTsz() { 
     return creationTsz; 
    } 

    /** 
    * 
    * @param creationTsz 
    *  The creation_tsz 
    */ 
    public void setCreationTsz(Integer creationTsz) { 
     this.creationTsz = creationTsz; 
    } 

    /** 
    * 
    * @return 
    *  The listingId 
    */ 
    public Integer getListingId() { 
     return listingId; 
    } 

    /** 
    * 
    * @param listingId 
    *  The listing_id 
    */ 
    public void setListingId(Integer listingId) { 
     this.listingId = listingId; 
    } 

    /** 
    * 
    * @return 
    *  The rank 
    */ 
    public Integer getRank() { 
     return rank; 
    } 

    /** 
    * 
    * @param rank 
    *  The rank 
    */ 
    public void setRank(Integer rank) { 
     this.rank = rank; 
    } 

    /** 
    * 
    * @return 
    *  The url75x75 
    */ 
    public String getUrl75x75() { 
     return url75x75; 
    } 

    /** 
    * 
    * @param url75x75 
    *  The url_75x75 
    */ 
    public void setUrl75x75(String url75x75) { 
     this.url75x75 = url75x75; 
    } 

    /** 
    * 
    * @return 
    *  The url170x135 
    */ 
    public String getUrl170x135() { 
     return url170x135; 
    } 

    /** 
    * 
    * @param url170x135 
    *  The url_170x135 
    */ 
    public void setUrl170x135(String url170x135) { 
     this.url170x135 = url170x135; 
    } 

    /** 
    * 
    * @return 
    *  The url570xN 
    */ 
    public String getUrl570xN() { 
     return url570xN; 
    } 

    /** 
    * 
    * @param url570xN 
    *  The url_570xN 
    */ 
    public void setUrl570xN(String url570xN) { 
     this.url570xN = url570xN; 
    } 

    /** 
    * 
    * @return 
    *  The urlFullxfull 
    */ 
    public String getUrlFullxfull() { 
     return urlFullxfull; 
    } 

    /** 
    * 
    * @param urlFullxfull 
    *  The url_fullxfull 
    */ 
    public void setUrlFullxfull(String urlFullxfull) { 
     this.urlFullxfull = urlFullxfull; 
    } 

    /** 
    * 
    * @return 
    *  The fullHeight 
    */ 
    public Integer getFullHeight() { 
     return fullHeight; 
    } 

    /** 
    * 
    * @param fullHeight 
    *  The full_height 
    */ 
    public void setFullHeight(Integer fullHeight) { 
     this.fullHeight = fullHeight; 
    } 

    /** 
    * 
    * @return 
    *  The fullWidth 
    */ 
    public Integer getFullWidth() { 
     return fullWidth; 
    } 

    /** 
    * 
    * @param fullWidth 
    *  The full_width 
    */ 
    public void setFullWidth(Integer fullWidth) { 
     this.fullWidth = fullWidth; 
    } 

    public Map<String, Object> getAdditionalProperties() { 
     return this.additionalProperties; 
    } 

    public void setAdditionalProperty(String name, Object value) { 
     this.additionalProperties.put(name, value); 
    } 

} 

ParamPagination分類に含まれますプロジェクトでも。

+0

はチェックhttp://json.parser.online.fr/ –

+0

であなたのJSONを確認してください。私はpojoの2つの異なるサービスを試してみた – Joao

答えて

1

問題はGsonConverterFactoryによって生成されます。解析するクラスのフィールドに注釈を付けないと、フィールド名はJSONのキーと正確に一致する必要があります。

たとえば、JSONのcreation_tszの場合、自動的に解析するには、クラスに同じ名前を付ける必要があります。あなたは2つのオプションがあり

は、 @Serisalizedアノテーション付きのクラスからフィールドに注釈を付ける、または hereからの値のいずれかに GsonConverterFactoryのフィールド名のポリシーを設定する方法を見つけます。あなたに最適なポリシーを使用してください。

私はいつもフィールドに注釈をつけています。ちょうどそれが愚かなものに注釈を付けて、それはライブラリからのバグではないからです。私はあなたが改造して、フィールド名のポリシーを変更する方法を見つけ

EDIT 1

Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES).create() 
Retrofit retrofit = new Retrofit.Builder() 
.baseUrl("https://openapi.etsy.com/v2/") 
.addConverterFactory(GsonConverterFactory.create(gson)) 
.build(); 
+0

ありがとう! @SerializedName( "url_fullxfull") プライベート文字列urlFullxfull;フィールド名url_fullxfull の例については、 の注釈を使用しています。 – Joao

+0

私の答えがあなたを助けたなら、それはあなたが望むならば、答えた:)(唯一のオフコースを望むならば) – danypata

+0

私は欲しいが、私は投票したが、私は評判の15ポイントを受け取ったときにのみマークする – Joao

関連する問題