简体   繁体   English

如何在我的 android 工作室项目中解析 object “rates”并从此 API(附图片)获取字符串“EUR”?

[英]How to parse the object “rates” and get the string “EUR” from this API (picture attached) in my android studio project?

This is the openexchangerate from where I want to parse the EUR Rate这是我要解析欧元汇率的 openexchangerate
图片

@Override
protected Void doInBackground(Void... voids) {
    try {
        String uRl = "https://openexchangerates.org/api/latest.json?app_id=9d60a61dd1914783b037cc5835ee9829&symbols=EUR";
        JSONObject reader = new JSONObject(uRl);
        JSONObject sys = reader.getJSONObject("rates");
        EUR = sys.getString("EUR");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    progressDialog.dismiss();
    textView.setText(EUR);
}

This is my code to parse the EUR Rate.这是我解析欧元汇率的代码。 I used a simple textview in xml to display the rate.我在 xml 中使用了一个简单的 textview 来显示速率。 But nothing is getting displayed in the emulator.但是模拟器中没有显示任何内容。 Could someone please help me where I am making mistake?有人可以帮我在哪里犯错吗?

Screenshot of my emulator我的模拟器截图
图片

The point is you're trying to parse the API URL to JSON object, instead of the API response. The point is you're trying to parse the API URL to JSON object, instead of the API response. You should call the API first, then parse the API response to JSON object.您应该先调用 API,然后解析 API 响应 JSON ZA8CFDE6331BD49EB66AC96F8911C。 A similar problem has been solved here: Use JSON API in Java类似的问题已经在这里解决了: Use JSON API in Java

Hope this is helpful.希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM