- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 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;
}
}
我收到服务器的响应,但并非所有字段都已填写。
响应模型(使用 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);
}
}
Param
和 Pagination
类也包含在项目中。
最佳答案
问题是由 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/
我试图将护照逻辑放入 Controller 文件中,但问题是当我将逻辑放入 Controller 中时,它告诉我“无法读取未定义的属性“主体””,但是当我将代码移至索引时,所有内容都会路由向右走 in
我正在学习 Javascript,我正在尝试创建一个简单的下拉菜单。我想要的功能的一个例子可以在谷歌主页的顶部菜单中看到,带有“更多”和“设置”下拉菜单。具体来说,当您单击关闭菜单时,菜单会消失。 我
我正在努力让 Swagger 正确呈现我的 ServiceStack 服务。 我希望看到一个 UserId 字符串作为表单参数,一个 PrivateCustomer 对象作为主体参数,但是尽管 Use
注意:由于随后的研究,这个问题已经完全重组。 我正在尝试从 Shiro 的主题 PrincipalCollection 中检索值.我在集合中添加了两个主体。 Username和 UUID .当我试图记
我们正在开发一个将 OAuth 2 用于两个用例的应用程序: 访问后端微服务(使用 client_credentials) 验证应用程序的用户(使用 authorization_code ,因此将用户
我有这段代码生成一个将 myNumber 乘以 5 的委托(delegate) ParameterExpression numParam = Expression.Parameter(typeof(i
我有一些jquery, $( document ).ready(function() { body=$(body).html; $("html").html(body); }); 这应
我创建了一个通用异常 DTO,它也扩展了 RuntimeException。通过这种方式,可以在应用程序中使用它,也可以将其用作 DTO。问题是当我将 DTO 应用于 ResponseEntity 构
在 Angular 5 HttpClient 中,我可以通过这种方式设置 HttpParams()。 const body = new HttpParams() .set('email', '
我正在从 RabbitMQ 读取数据,如下所示: connection = factory.newConnection(); ch = connection.createChannel() ; Str
如何使用不同类型的调用和响应主体来改造 PUT?我有一个错误限制。类型必须相同 and 。响应bodie可以包含int值,但call不应该,因为当我用int值初始化CallBody对象时,它已经包
原则上我想做这样的事情: #grab some value from outer source (i.e. file or list defined by another programer) set
我知道如何使用TextureRegions 创建动画并将其应用于非box2d 游戏中的对象。 但是在 libgdx 的 box2d 中,我不知道该怎么做。在CocosD2中,Sprite对象中有run
我有这段代码生成一个将 myNumber 乘以 5 的委托(delegate) ParameterExpression numParam = Expression.Parameter(typeof(i
我已经计算了花括号的数量,但无法弄清楚为什么类主体不完整。每次我试图修复类(class)时,都会把整个类(class)弄乱。问题出在代码中的最后一个类。最后一个花括号给我带来了类里面的麻烦。我正在使用
有人知道吗?我只能看到 ApplyTorque 和 SetAngularVelocity,我只想在将对象添加到模拟之前旋转对象,例如:所以我有一个 crate 倾斜靠在墙上,另一个 crate 是平的
我可以获得如何让图像出现在 box2d 主体上的简单答案吗?我尝试为图像和主体创建 x 和 y int,但是一旦主体移动,图像就会保持静态。如果您确实回答,请尽可能解释一下代码。如果您对我的完整源代码
我知道我可以通过使用 PolygonRegion 来做到这一点,但问题是我使用 scene2d.Stage 和几个 Actor 。您可能知道阶段使用 SpriteBatch 而我无法渲染 Polygo
您好,我有以下代码: function redirect(){ window.location.href='logged_out_chat.php'; } ...在我的标题和以下正文标记中:
我在 didBegin(contact:) 中触发了 SpriteKit 物理接触。我为要移出屏幕的 Dot 对象的实例抓取物理体,但是当我尝试像这样更改其位置时,没有任何反应: 第一种方法 /* I
我是一名优秀的程序员,十分优秀!