改型返回LinkedTreeMap而不是JSON

改型返回LinkedTreeMap而不是JSON,第1张

概述我正在使用Retrofit2.0从Web服务读取数据,该服务返回json文本,但是在以下代码中响应为LinkedTreeMap,我无法将其转换为json:我的介面publicinterfaceGeoAPIInterface{StringENDPOINT="http://www.geoplugin.net/";@GET("json.gp")Call<Object>getIP(@Que

我正在使用Retrofit 2.0从Web服务读取数据,该服务返回Json文本,但是在以下代码中响应为linkedTreeMap,我无法将其转换为Json:

我的介面

public interface GeoAPIInterface {    String ENDPOINT = "http://www.geoplugin.net/";    @GET("Json.gp")    Call<Object> getIP(@query("ip") String ip);}

现在我从站点读取数据:

Retrofit retrofit = new Retrofit.Builder()         .baseUrl(GeoAPIInterface.ENDPOINT)         .addConverterFactory(GsonConverterFactory.create())         .build();GeoAPIInterface inBankAPI = retrofit.create(GeoAPIInterface.class);Call<Object> call=inBankAPI.getIP("8.8.8.8");call.enqueue(new Callback<Object>() {    @OverrIDe    public voID onResponse(Response<Object> response, Retrofit retrofit) {        Log.d("App","");    }    @OverrIDe        public voID onFailure(Throwable t) {            Log.d("App","error!");    }});

形成的网址是http://www.geoplugin.net/Json.gp?ip=8.8.8.8
Json响应是

{  "geoplugin_request":"8.8.8.8",  "geoplugin_status":200,  "geoplugin_credit":"Some of the returned data includes Geolite data created by maxmind, available from <a href=\'http:\/\/www.maxmind.com\'>http:\/\/www.maxmind.com<\/a>.",  "geoplugin_city":"Mountain VIEw",  "geoplugin_region":"CA",  "geoplugin_areaCode":"650",  "geoplugin_dmaCode":"807",  "geoplugin_countryCode":"US",  "geoplugin_country@R_502_6889@":"United States",  "geoplugin_continentCode":"NA",  "geoplugin_latitude":"37.386002",  "geoplugin_longitude":"-122.083801",  "geoplugin_regionCode":"CA",  "geoplugin_region@R_502_6889@":"California",  "geoplugin_currencyCode":"USD",  "geoplugin_currencySymbol":"&#36;",  "geoplugin_currencySymbol_UTF8":"$",  "geoplugin_currencyConverter":1}

解决方法:

替换您的Response< Object>带有Response< GeoData>哪里

public class GeoData{    private String geoplugin_regionCode;    private String geoplugin_city;    private String geoplugin_credit;    private String geoplugin_currencyConverter;    private String geoplugin_currencyCode;    private String geoplugin_region;    private String geoplugin_currencySymbol;    private String geoplugin_region@R_502_6889@;    private String geoplugin_country@R_502_6889@;    private String geoplugin_currencySymbol_UTF8;    private String geoplugin_areaCode;    private String geoplugin_dmaCode;    private String geoplugin_continentCode;    private String geoplugin_request;    private String geoplugin_latitude;    private String geoplugin_longitude;    private String geoplugin_countryCode;    private String geoplugin_status;    public String getGeoplugin_regionCode () {        return geoplugin_regionCode;    }    public voID setGeoplugin_regionCode (String geoplugin_regionCode) {        this.geoplugin_regionCode = geoplugin_regionCode;    }    public String getGeoplugin_city () {        return geoplugin_city;    }    public voID setGeoplugin_city (String geoplugin_city) {        this.geoplugin_city = geoplugin_city;    }    public String getGeoplugin_credit () {        return geoplugin_credit;    }    public voID setGeoplugin_credit (String geoplugin_credit) {        this.geoplugin_credit = geoplugin_credit;    }    public String getGeoplugin_currencyConverter () {        return geoplugin_currencyConverter;    }    public voID setGeoplugin_currencyConverter (String geoplugin_currencyConverter) {        this.geoplugin_currencyConverter = geoplugin_currencyConverter;    }    public String getGeoplugin_currencyCode () {        return geoplugin_currencyCode;    }    public voID setGeoplugin_currencyCode (String geoplugin_currencyCode) {        this.geoplugin_currencyCode = geoplugin_currencyCode;    }    public String getGeoplugin_region () {        return geoplugin_region;    }    public voID setGeoplugin_region (String geoplugin_region) {        this.geoplugin_region = geoplugin_region;    }    public String getGeoplugin_currencySymbol () {        return geoplugin_currencySymbol;    }    public voID setGeoplugin_currencySymbol (String geoplugin_currencySymbol) {        this.geoplugin_currencySymbol = geoplugin_currencySymbol;    }    public String getGeoplugin_region@R_502_6889@ () {        return geoplugin_region@R_502_6889@;    }    public voID setGeoplugin_region@R_502_6889@ (String geoplugin_region@R_502_6889@) {        this.geoplugin_region@R_502_6889@ = geoplugin_region@R_502_6889@;    }    public String getGeoplugin_country@R_502_6889@ () {        return geoplugin_country@R_502_6889@;    }    public voID setGeoplugin_country@R_502_6889@ (String geoplugin_country@R_502_6889@) {        this.geoplugin_country@R_502_6889@ = geoplugin_country@R_502_6889@;    }    public String getGeoplugin_currencySymbol_UTF8 () {        return geoplugin_currencySymbol_UTF8;    }    public voID setGeoplugin_currencySymbol_UTF8 (String geoplugin_currencySymbol_UTF8) {        this.geoplugin_currencySymbol_UTF8 = geoplugin_currencySymbol_UTF8;    }    public String getGeoplugin_areaCode () {        return geoplugin_areaCode;    }    public voID setGeoplugin_areaCode (String geoplugin_areaCode) {        this.geoplugin_areaCode = geoplugin_areaCode;    }    public String getGeoplugin_dmaCode () {        return geoplugin_dmaCode;    }    public voID setGeoplugin_dmaCode (String geoplugin_dmaCode) {        this.geoplugin_dmaCode = geoplugin_dmaCode;    }    public String getGeoplugin_continentCode () {        return geoplugin_continentCode;    }    public voID setGeoplugin_continentCode (String geoplugin_continentCode) {        this.geoplugin_continentCode = geoplugin_continentCode;    }    public String getGeoplugin_request () {        return geoplugin_request;    }    public voID setGeoplugin_request (String geoplugin_request) {        this.geoplugin_request = geoplugin_request;    }    public String getGeoplugin_latitude () {        return geoplugin_latitude;    }    public voID setGeoplugin_latitude (String geoplugin_latitude) {        this.geoplugin_latitude = geoplugin_latitude;    }    public String getGeoplugin_longitude () {        return geoplugin_longitude;    }    public voID setGeoplugin_longitude (String geoplugin_longitude) {        this.geoplugin_longitude = geoplugin_longitude;    }    public String getGeoplugin_countryCode () {        return geoplugin_countryCode;    }    public voID setGeoplugin_countryCode (String geoplugin_countryCode) {        this.geoplugin_countryCode = geoplugin_countryCode;    }    public String getGeoplugin_status () {        return geoplugin_status;    }    public voID setGeoplugin_status (String geoplugin_status) {        this.geoplugin_status = geoplugin_status;    }}

并且您已经在此对象中拥有所有参数,则无需转换和解析JsON.
Here您可以从JsON获取对象.

总结

以上是内存溢出为你收集整理的改型返回LinkedTreeMap而不是JSON全部内容,希望文章能够帮你解决改型返回LinkedTreeMap而不是JSON所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1094103.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存