我正在尝试获取JSON格式http://httpbin.org/get。它看起来像
{
"args": {},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,* /*;q=0.8",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "pl,en-US;q=0.7,en;q=0.3",
"Cookie": "_ga=GA1.2.1591535064.1485716088; _gat=1",
"Host": "httpbin.org",
"Referer": "http://httpbin.org/",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0"
},
"origin": "83.10.45.163",
"url": "http://httpbin.org/get"
}
我使用http://www.jsonschema2pojo.org/将JSON转换为POJO:
-----------------------------------com.app.apitest.test.Args.java-----------------------------------
package com.app.apitest.test;
public class Args {
}
-----------------------------------com.app.apitest.test.Example.java-----------------------------------
package com.app.apitest.test;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Example {
@SerializedName("args")
@Expose
private Args args;
@SerializedName("headers")
@Expose
private Headers headers;
@SerializedName("origin")
@Expose
private String origin;
@SerializedName("url")
@Expose
private String url;
public Args getArgs() {
return args;
}
public void setArgs(Args args) {
this.args = args;
}
public Headers getHeaders() {
return headers;
}
public void setHeaders(Headers headers) {
this.headers = headers;
}
public String getOrigin() {
return origin;
}
public void setOrigin(String origin) {
this.origin = origin;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
-----------------------------------com.app.apitest.test.Headers.java-----------------------------------
package com.app.apitest.test;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Headers {
@SerializedName("Accept")
@Expose
private String accept;
@SerializedName("Accept-Encoding")
@Expose
private String acceptEncoding;
@SerializedName("Accept-Language")
@Expose
private String acceptLanguage;
@SerializedName("Cookie")
@Expose
private String cookie;
@SerializedName("Host")
@Expose
private String host;
@SerializedName("Referer")
@Expose
private String referer;
@SerializedName("Upgrade-Insecure-Requests")
@Expose
private String upgradeInsecureRequests;
@SerializedName("User-Agent")
@Expose
private String userAgent;
public String getAccept() {
return accept;
}
public void setAccept(String accept) {
this.accept = accept;
}
public String getAcceptEncoding() {
return acceptEncoding;
}
public void setAcceptEncoding(String acceptEncoding) {
this.acceptEncoding = acceptEncoding;
}
public String getAcceptLanguage() {
return acceptLanguage;
}
public void setAcceptLanguage(String acceptLanguage) {
this.acceptLanguage = acceptLanguage;
}
public String getCookie() {
return cookie;
}
public void setCookie(String cookie) {
this.cookie = cookie;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getReferer() {
return referer;
}
public void setReferer(String referer) {
this.referer = referer;
}
public String getUpgradeInsecureRequests() {
return upgradeInsecureRequests;
}
public void setUpgradeInsecureRequests(String upgradeInsecureRequests) {
this.upgradeInsecureRequests = upgradeInsecureRequests;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
}
但是当我得到Result时,我的Example.class没有正确填充。有一些空字段...... 看看SS:
有什么问题?
答案 0 :(得分:0)
这是因为标题(和arg)不是真正的固定字段集。它是Map<String, String>
。而你的Android客户端不会发送那些字段。