- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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!");
}
});
形成的 url 是 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_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":"$",
"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_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 中获取对象。
关于android - Retrofit 返回 LinkedTreeMap 而不是 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33720204/
我在序列化 JSON 数据时遇到问题。 所以,我尝试使用泛型,但不知何故,当我尝试实现它时,Gson 讨厌我。我想做的就是对泛型进行基本抽象。这是我的代码: 执行泛型部分的主类: package co
我只是想从我的 Gson 对象中获取值,所以在我的“城市”对象中: 我需要从“isSelected”中获取名称“first”和 bool 值。 我刚刚写了一个新类“KeyPairBoolData”:
我尝试在 Android 应用程序中使用 REST 服务,但在序列化 ArrayList 时遇到问题: 我的 RESTful 服务返回: 我的 Android 应用程序收到: 我正在使用 Gson 库
在标准 JDK 或支持排序和插入顺序的简单独立库中是否有可用的 LinkedTreeMap 实现?我找到了 gridsim有一个 LinkedTreeMap implementation .但它似乎是
我正在使用 Retrofit 2.0 从返回 json 文本的 Web 服务读取数据,但在以下代码中响应是 LinkedTreeMap,我无法将其转换为 json: 我的界面 public inter
我有一个如下所示的 JSON 结构: { "identifier": 1045608, "scientificName": "Apis mellifera Linnaeus 1758",
我正在尝试使我的响应类通用。问题是它返回一个 com.google.gson.internal.LinkedTreeMap 类型的列表,而不是 Brewer 类型。 启动这一切的函数: List br
我最近开始研究 JSON 字符串,有人告诉我 Google 自己的库 Gson 是处理这些字符串的新潮方式。 我的理解是,JSON 字符串本质上是一张 map 。其中每个变量指向字符串中的一个值。 例
对于 java 数据处理程序,我发送格式正确的 JSON,但 Spring、Java 决定如何转换它看到的内容以及框架的组合我真的不应该去改变破坏 JSON,以便一旦我能看到它,它变成了 Linked
我知道以前有人问过这个问题。由于我在java和android方面的新手技能。我无法解决这个问题超过一个星期。 我和我的一个 friend 正在开发一个 android 项目,如果有一些这样的事情。 这
我正在尝试通过 Retrofit 检索对象列表: public interface TemplateApi { @GET("/api/templates") Observable> g
这个问题已经有答案了: 奥 git _a (13 个回答) 已关闭 3 年前。 我在从 JSON 字符串获取对象时遇到一些问题。 我得到了 Product 类 public class Product
我第一次使用 GSON 进行改造。它在静态数据上运行流畅,但在动态数据的情况下显示上述错误。在设置文本值时在适配器中显示错误 主 Activity public class MainActivity
我正在尝试读取以下 json 数据。如何使用 LinkedTreeMap 进行阅读? {"msgType": "gameInit", "data": { "race": { "track"
下面这行代码对我来说很正常: val users: Array = com.google.gson.Gson().fromJson(data, Array::class.java) 但是当我启用 Pr
我是 kotlin 新手,刚刚将我的代码从 java 迁移到 kotlin。我有一个数据类 HistoryList,用于解析来自 API 的响应。 data class HistoryList(
我正在尝试通过广播在 Intent 中传递包含 Analytics Reporting 数据的对象。问题是反序列化返回 LinkedTreeMap 而不是原始序列化对象,导致 ClassCastExc
这个问题在这里已经有了答案: Gson TypeToken with dynamic ArrayList item type (13 个回答) 关闭3年前。 我在从 JSON 字符串中获取对象时遇到了
import com.google.gson.Gson import com.google.gson.reflect.TypeToken fun main() { val jsonString
我正在使用 Retrofit+Gson 来解析 JSON。 当我尝试解析来自 Google Places API 的响应时(好吧,我不尝试解析,我只是尝试为该响应创建模型),但出现了一些错误。 这是来
我是一名优秀的程序员,十分优秀!