【发布时间】:2017-12-24 17:24:38
【问题描述】:
亲爱的。
我能够从 Alpha Vantage 检索 JSON 对象以在我的货币转换器应用程序中使用,但我无法查找我想要的字符串值(即“5. Exchange Rate”:“17.86300000”),因为标识符有如下空格:
{ “实时货币汇率”:{ "1. From_Currency Code": "USD", "2. From_Currency Name": "美元", "3. To_Currency Code": "EGP", "4. To_Currency Name": "埃及镑", "5. 汇率": "17.86300000", "6.上次刷新": "2017-12-24 14:38:20", “7.时区”:“UTC” } }
下面是我的代码来捕获字符串值
@Override
protected String doInBackground(String... f_url) {
String urlStr = "https://www.alphavantage.co/query?function=CURRENCY_EXCHANGE_RATE&from_currency="
+ textto.getText()
+ "&to_currency="
+ textfrom.getText()
+ "&apikey=XXXXXXXXX";
String data="";
String converted="";
try {
URL url = new URL(urlStr);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line ="";
while (line != null)
{
line=bufferedReader.readLine();
data = data +line;
}
JSONArray JA = new JSONArray(data);
JSONObject JO = (JSONObject) JA.get(0);
converted = JO.getString("5. Exchange Rate");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return converted;
}
该方法在logcat中没有异常返回空白值。我已经用另一个标识符中没有空格的 JSON 对象测试了代码并且运行良好。
任何帮助我在这里缺少什么?
【问题讨论】:
-
你能发布你的整个响应 json
-
如果您在帖子中显示的 JSON 字符串代表您得到的内容,那么我看不到该数组。换句话说
JSONArray JA = new JSONArray(data);不应该工作。请向我们展示您在“数据”中收到的 JSON 字符串的示例。 -
我已经更新了获取我在“数据”中获得的值的问题
-
这也是我获取 JSON 的 URL:alphavantage.co/…
标签: android json android-asynctask