親愛なる。
Alpha VantageからJSONオブジェクトを取得して、通貨コンバータアプリケーションで使用することはできますが、識別子に次のようなスペースがあるため、必要な文字列値(つまり、「5。為替レート」:「17.86300000」)を検索できません。未満:
{"リアルタイム為替レート":{"1。From_Currency Code": "USD"、 "2. From_Currency Name": "United States Dollar"、 "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に配列がありません。代わりにこれを試してください:
JSONObject reader = new JSONObject(data);
JSONObject sys = reader.getJSONObject("Realtime Currency Exchange Rate");
String currency = sys.getString("5. Exchange Rate");
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加