gpt4 book ai didi

java - 改造响应主体接收到具有空字段的一半对象

转载 作者:行者123 更新时间:2023-11-30 01:15:04 27 4
gpt4 key购买 nike

我正在尝试使用 Retrofit 获取对象列表,但响应正文是半空的并且列表包含空字段。服务器正在发送正确的响应,但我仍然没有在应用程序中获得这些值。如何解决?

我的 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": {}
}

我尝试获取 REST 查询的类:

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 类也包含在项目中。

最佳答案

问题是由 GsonConverterFactory 产生的,如果您不注释将要解析的类的字段,则字段名称应与 JSON 中的键完全匹配。

例如JSON中的creation_tsz,你必须在类中有相同的名称才能自动解析它。

您有两个选择,使用 @Serisalized 注释对类中的字段进行注释,或者找到一种方法将 GsonConverterFactory 的字段名称策略设置为来自here .使用最适合您的政策。

只是一个旁注,我总是注释字段,只是为了确保这是因为我注释了一些愚蠢的东西,而不是库中的错误。

编辑 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();

关于java - 改造响应主体接收到具有空字段的一半对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37968723/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com