我正在尝试从Earthquake API获取数据。
我正在使用翻新。数据为XML
格式。我可以很好地获得XML格式的数据。我使用XML-JSON Converter Library将其转换为JSON格式。当我用一个简单的xml字符串尝试它时,这个库工作得很好
<title>Hello</title>
。
该库将其真正转换为JSON格式。
但是在转换从api获取的数据时,它给出了这个错误:
java.lang.NoSuchMethodError: No virtual method end()Z in class Lorg/json/XMLTokener
这个xml肯定有什么特别之处。请先查看接口响应。
这些是我的文件: MainActivity.Java
public class MainActivity extends AppCompatActivity {
private static final String TAG = "sagar";
private ApiInterface apiInterface;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
apiInterface = ApiClient.getClient().create(ApiInterface.class);
apiInterface.getTodos().enqueue(new Callback < ResponseBody > () {
@Override
public void onResponse(Call < ResponseBody > call, Response < ResponseBody > response) {
try {
String xmlStr = response.body().string();
System.out.println("Response is => " + xmlStr.trim());
JSONObject jsonObject = XML.toJSONObject(xmlStr);
System.out.println("Response -> " + jsonObject);
} catch (JSONException | IOException e) {
e.printStackTrace();
}
// System.out.println("Response -> " + response.body());
}
@Override
public void onFailure(Call < ResponseBody > call, Throwable t) {
System.out.println("Failure -> " + t.getMessage());
}
});
}
}
APIClient.java
public class ApiClient {
private static final String BASE_URL = "https://earthquake.usgs.gov/";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
ApiInterface.java
public interface ApiInterface {
@GET("earthquakes/feed/v1.0/summary/significant_day.atom")
Call < ResponseBody > getTodos();
}
发布于 2021-04-27 00:57:18
只需将url中的.atom
替换为.geojson
https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson
输出:
{
"type": "FeatureCollection",
"metadata": {
"generated": 1619456055000,
"url":"https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson",
"title": "USGS Significant Earthquakes, Past Day",
"status": 200,
"api": "1.10.3",
"count": 1
},
"features": [
{
"type": "Feature",
"properties": {
"mag": 6.4,
"place": "200 km WSW of Haveluloto, Tonga",
"time": 1619389680681,
"updated": 1619449698040,
"tz": null,
"url": "https://earthquake.usgs.gov/earthquakes/eventpage/us6000e4rl",
"detail": "https://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/us6000e4rl.geojson",
"felt": 1,
"cdi": 2.7,
"mmi": 3.689,
"alert": "green",
"status": "reviewed",
"tsunami": 0,
"sig": 630,
"net": "us",
"code": "6000e4rl",
"ids": ",us6000e4rl,",
"sources": ",us,",
"types": ",dyfi,losspager,moment-tensor,origin,phase-data,shakemap,",
"nst": null,
"dmin": 6.009,
"rms": 1.12,
"gap": 21,
"magType": "mww",
"type": "earthquake",
"title": "M 6.4 - 200 km WSW of Haveluloto, Tonga"
},
"geometry": {
"type": "Point",
"coordinates": [-177.0771,-21.6472,234.29]
},
"id": "us6000e4rl"
}
]
}
https://stackoverflow.com/questions/67270351
复制相似问题