I have some error parse Json on Android Studio, Please if you have any idea can you share where is my error.
I want to get the current exchange rate information and print it on textview.
Json/Api Url: https://api.exchangeratesapi.io/latest?base=USD
private void jsonParse() {
String SHOP_URL = "https://api.exchangeratesapi.io/latest?base=USD&symbols=EUR,GBP";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, SHOP_URL, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("rates");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject result = jsonArray.getJSONObject(i);
int xGBP=result.getInt("GBP");
usdtry.setText(String.valueOf(xGBP));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
pRequestQueue.add(request);
}
This code not erroring but not response any..I think i have some missing.
rates
is a JsonObject
, not an Array.
So, your code must be
JSONObject rates = response.getJSONObject("rates");
double xGBP = rates.getDouble("GBP");
usdtry.setText(String.valueOf(xGBP));
Also, for better formatting, you can use:
DecimalFormat formatter = new DecimalFormat("###,###.##", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
usdtry.setText(formatter.format(xGBP));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.