Reputation: 658
I am trying to fetch the data from Earthquake API.
I am using retrofit. The data is in XML
format. I am getting the data fine in XML. I am converting it in JSON using a XML-JSON Converter Library. And the library works just fine when i tried it with a simple xml string such as
<title>Hello</title>
.
And the library really converts it in JSON.
But while converting the data fetched from the api, it gives me this error:
java.lang.NoSuchMethodError: No virtual method end()Z in class Lorg/json/XMLTokener
Definitely there's something with this xml. Please checkout the API response first.
These are my files: 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();
}
Upvotes: 1
Views: 133
Reputation: 12900
Just replace .atom
in the url with .geojson
https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_day.geojson
Output:
{
"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"
}
]
}
Upvotes: 1