Retrofit 返回 LinkedTreeMap 而不是 JSON

2023-11-29

我正在使用 Retrofit 2.0 从返回 json 文本的 Web 服务读取数据,但在以下代码中响应是 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.8json 响应是

{
  "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_countryName":"United States",
  "geoplugin_continentCode":"NA",
  "geoplugin_latitude":"37.386002",
  "geoplugin_longitude":"-122.083801",
  "geoplugin_regionCode":"CA",
  "geoplugin_regionName":"California",
  "geoplugin_currencyCode":"USD",
  "geoplugin_currencySymbol":"&#36;",
  "geoplugin_currencySymbol_UTF8":"$",
  "geoplugin_currencyConverter":1
}

更换你的Response<Object> with Response<GeoData> where

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_regionName;
    private String geoplugin_countryName;
    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_regionName () {
        return geoplugin_regionName;
    }
    public void setGeoplugin_regionName (String geoplugin_regionName) {
        this.geoplugin_regionName = geoplugin_regionName;
    }
    public String getGeoplugin_countryName () {
        return geoplugin_countryName;
    }
    public void setGeoplugin_countryName (String geoplugin_countryName) {
        this.geoplugin_countryName = geoplugin_countryName;
    }
    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 中获取对象。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Retrofit 返回 LinkedTreeMap 而不是 JSON 的相关文章

  • Android Studio 3.1.3不显示布局设计预览

    自从我上次在 android studio 中创建一个新项目以来已经有一段时间了 今天当我这样做时 android studio 没有从布局预览中显示任何内容 它是一个空白窗口 并显示 没有可显示的内容 我已经尝试过了 更新了sdks工具
  • 运行带有 QVGA 的 Android 1.6 的模拟器上的缩放控件

    我正在模拟器中的不同 AVD 设置上测试我的应用程序 并在仅使用 QVGA 分辨率 lcd 密度 120 的 Android 1 6 上的 MapView 中遇到缩放控件问题 当点击屏幕以显示缩放控件时 放大 加号显示得比缩小大得多 这似乎
  • 不破坏“$ref”引用的 JSON 树修改

    问题与 JSON 相关 它是序列化的结果保留引用处理 set to 保留ReferencesHandling Objects 我正在寻找巧妙的方法来修改 JSON 树的分支 特别是删除或替换分支 以便引用处理不会被破坏 考虑以下 JSON
  • 有人知道如何在android中实现像Unfold(这是iphone中的应用程序)这样的效果吗?

    我怎样才能实现这个效果呢 任何建议都会对我有帮助 None
  • 无法解析符号“AuthUI”

    我正在添加 firebase google 和 Gmail 登录 但是当我粘贴代码时 onAuthStateChanged NonNull FirebaseAuth firebaseAuth 方法我收到此错误 无法解析符号 AuthUI 在
  • 使用数据绑定更新对象类型 LiveData

    我想通过与 livedata 的数据绑定来更新视图 让我们看一下场景 数据类 data class Movie var name String var createdAt String 视图模型 class MyViewModel View
  • 导入的项目抛出 java.lang.ClassNotFoundException

    所以我将一个现有的项目导入到 eclipse 中 但让它工作时遇到了很多麻烦 该项目与其他三个图书馆项目一起提供 谷歌播放服务库 捕获活动 肖像二维码 As soon as I imported the project I went und
  • 创建像 facebook android 一样的登录动画

    我想创建一个登录页面 如 facebook android 应用程序 其中包含用户名和密码EditText字段被隐藏 页面上会显示一个徽标 该徽标会在上方动画一定距离并停留在新位置 然后显示编辑字段 这是我尝试过的 但这里的徽标从页面底部开
  • 如何从 Android 服务获取应用程序上下文?

    我有一个正在运行并监听麦克风输入的 Android 服务 我希望它在满足特定条件时启动一项活动 为了创建意图 我需要应用程序上下文 我怎么才能得到它 Intent i new Intent ctx SONR class i addFlags
  • DP5 7.0 - 向待处理意图添加额外内容是否会失败?

    在跟踪器上添加链接的问题 因此 我今天在我的 Nexus 5X 上安装了 DP5 Android 7 0 版本 我一直在开发一个应用程序 它使用 Android 的 AlarmManager 类在特定时间安排本地通知 在此版本之前 代码在运
  • adb 无法启动守护进程,CreateProcess 失败,错误 2

    我无法运行adb root或任何 adb 命令 我收到以下错误 我设置ADB TRACE 1 C WINDOWS system32 gt adb root system core adb adb c main Handling comman
  • 使用 KitKat 存储访问框架后打开 Google Drive 文件内容 URI

    我正在使用 android 4 4 的存储访问框架并打开文件选择器 一切正常 除了从 Google Drive 选择文件时 我只能弄清楚如何将其作为输入流打开 但我想获得一个 java File 对象 返回的内容 uri 如下所示 cont
  • 为什么 CheckBox 检查不能以编程方式与 Kotlin 一起使用?

    我想这个问题以前可能有人问过 但这个问题也发生在我身上 所以我在这里再次询问 看看我们能否找到解决方案 所以基本上问题是以编程方式检查复选框不与 Kotlin 代码一起工作 为了解释一下 我正在分享我的代码和问题的屏幕截图 filterCo
  • 连续按钮的自定义 arrayadapter 和 onclicklistener

    我有一个自定义数组适配器 我想为每一行中的按钮添加一个 onclicklistener 当我单击按钮时 我希望图像资源发生变化 一切正常 除了当我单击按钮时图像发生变化但另一行中的另一个按钮的图像也会发生变化 感谢您的帮助 这是我的代码 p
  • 从包中获取参数

    我正在尝试将参数从我的活动传递到片段 并且我正在使用以下代码 Override protected void onCreate Bundle savedInstanceState super onCreate savedInstanceSt
  • 将 mp3 添加到 ContentResolver

    我知道从您的应用程序下载 mp3 后 您需要将其添加到ContentResolver在音乐播放器上看到它 我正在使用以下代码执行此操作 private void addFileToContentProvider ContentValues
  • 如何在android中动态创建按钮?

    我想创建一个这样的页面 这 7 个按钮已经存在 但如果用户想添加更多类别 按钮 那么他可以使用 按钮并使用删除 按钮 有制作这个的想法或教程吗 创建 删除按钮onClick of button and button如下 public voi
  • 无法使用 Alamofire 在 swift 3 中上传图像

    我在尝试让 Alamofire 上传图像时被困了三天 这个想法是 Alamofire 将其与一些 php 代码一起发送到服务器 经过大量尝试和查看不同的地方后 一些代码应该可以工作 但是 Alamofire 的服务器端文档很糟糕 最近对 s
  • SDK管理器不显示示例包选项

    我关注的是安卓安装示例的开发人员链接 http developer android com tools samples index html包裹 但是 当我展开Android平台的包列表时 没有 SDK示例 选项 复选框 可用 为什么 我已
  • 从 C# 应用程序调用 ASP.net Web 服务

    我有个问题 我如何调用 Web 服务并从 C 桌面应用程序获取结果 我正在制作一个桌面应用程序 我希望它能够连接到我的在线 ASP net Web 服务 这怎么可能 在 解决方案资源管理器 中 右键单击项目节点并选择 添加 Service参

随机推荐