我正在尝试从该网站获取所有货币汇率并在我的应用程序中进行处理。这是网站:https ://raw.github.com/currencybot/open-exchange-rates/master/latest.json
我正在使用这个类来获取所有的货币汇率,但问题是它以随机顺序获取它,因为 Iterator keys = ratesObj.key();
我该如何解决这个问题,以便它按顺序获得所有货币汇率?
这是代码:
private class DownloadWebPageTask extends AsyncTask<String, Void, ArrayList<Double>>
{
protected ArrayList<Double> doInBackground(String... urls)
{
ArrayList<Double> response = new ArrayList<Double>();
for (String url : urls)
{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try
{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
InputStreamReader isr = new InputStreamReader(content);
StringBuilder sb = new StringBuilder();
char[] buffer = new char[1024];
while (isr.read(buffer) != -1)
{
sb.append(buffer);
}
JSONObject jobj = new JSONObject(sb.toString());
JSONObject ratesObj = jobj.getJSONObject("rates");
Iterator<String> keys = ratesObj.keys();
while (keys.hasNext())
{
response.add(Double.parseDouble(ratesObj.getString(keys.next())));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return response;
}
@Override
protected void onPostExecute(ArrayList<Double> result)
{
currency_values = result;
}
}
感谢大家的帮助!对此,我真的非常感激。
更新:这是现在的代码。
try
{
HttpResponse execute = client.execute(httpGet);
InputStream content = execute.getEntity().getContent();
String s = EntityUtils.toString(execute.getEntity());
JSONObject jobj = new JSONObject(s);
JSONObject ratesObj = jobj.getJSONObject("rates");
Iterator<String> keys = ratesObj.keys();
while (keys.hasNext())
{
response.add(Double.parseDouble(ratesObj.getString(keys.next())));
}
} catch (Exception e) {
e.printStackTrace();
}