我正在解析来自json restful webservice的json数据,并给出了这个结果
{
"RestResponse" : {
"result" : {
"countryIso2" : "US",
"stateAbbr" : "CA",
"postal" : "94043",
"continent" : "North America",
"state" : "California",
"longitude" : "-122.0574",
"latitude" : "37.4192",
"ds" : "II",
"network" : "AS15169 Google Inc.",
"city" : "Mountain View",
"country" : "United States",
"ip" : "172.217.3.14"
}
}
}
所以我的pojos是这样的:
public class TestIpAdress {
@SerializedName("RestResponse")
private RestResponse restResponse;
public RestResponse getRestResponse() {
return restResponse;
}
public void setRestResponse(RestResponse restResponse) {
this.restResponse = restResponse;
}
}
public class RestResponse {
@SerializedName("result")
private Result result;
public Result getResult() {
return result;
}
public void setResult(Result result) {
this.result = result;
}
}
public class Result {
@SerializedName("countryIso2")
private String countryIso2;
@SerializedName("postal")
private String postal;
@SerializedName("continent")
private String continent;
@SerializedName("state")
private String state;
@SerializedName("longitude")
private Double longitude;
@SerializedName("latitude")
private Double latitude;
@SerializedName("ds")
private String ds;
@SerializedName("ip")
private String ip;
@SerializedName("city")
private String city;
@SerializedName("country")
private String country;
public String getCountryIso2() {
return countryIso2;
}
public void setCountryIso2(String countryIso2) {
this.countryIso2 = countryIso2;
}
public String getPostal() {
return postal;
}
public void setPostal(String postal) {
this.postal = postal;
}
public String getContinent() {
return continent;
}
public void setContinent(String continent) {
this.continent = continent;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public String getDs() {
return ds;
}
public void setDs(String ds) {
this.ds = ds;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
在Android应用中,我使用以下内容来检索和解析Web服务输出:
StrictMode.ThreadPolicy policy = new
StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String link = "http://geo.groupkt.com/ip/172.217.3.14/json";
URL url = new URL(link);
HttpURLConnection urlConnection =
(HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(inputStream);
Gson gson = new Gson();
RestResponse restResponse = gson.fromJson(reader, new
TypeToken<RestResponse>(){}.getType());
Result myPosition = restResponse.getResult();
editText.setText("My city :\n" + myPosition.getCity());
但是我总是在“myPosition”对象中得到一个空值,有什么建议吗?
答案 0 :(得分:0)
您的json只有一个名为RestResponse
的属性,您需要使用具有此属性的类,例如TestIpAdress
。
TestIpAdress response = gson.fromJson(reader, TestIpAdress.class);
RestResponse restResponse = response.getRestResponse();