2016-12-18 4 views
-1

Я разбор данных JSON с JSON успокоительных WebService Wich дает этому результатуgson возвращающиеся нулевых значения из успокоительной WebService

{ 
    "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 приложения , Я использую следующее для извлечения и анализа вывода веб-службы:

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

Ваш json имеет только атрибут с именем RestResponse, вам нужно использовать класс, который имеет этот атрибут, например, ваш TestIpAdress.

TestIpAdress response = gson.fromJson(reader, TestIpAdress.class); 
RestResponse restResponse = response.getRestResponse(); 
Смежные вопросы