0

我正在尝试从该网站获取所有货币汇率并在我的应用程序中进行处理。这是网站: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();
            }
4

1 回答 1

1

这是实现我在评论中建议的一种方法,使用 aSortedMap<String, Double>将三字母货币代码映射到汇率,同时按货币代码按字母顺序排列:

  protected SortedMap<String, Double> doInBackground(String... urls) {
    SortedMap<String, Double> result = new TreeMap<String, Double>();

    HttpClient client = new DefaultHttpClient();
    for (String url : urls) {
      try {
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);
        String jsonString = EntityUtils.toString(response.getEntity());

        JSONObject json = new JSONObject(jsonString);
        JSONObject rates = json.getJSONObject("rates");
        Iterator<String> currencyCodes = rates.keys();
        while (currencyCodes.hasNext()) {
          String currencyCode = currencyCodes.next();
          double rate = rates.getDouble(currencyCode);
          result.put(currencyCode, rate);
        }
      } catch (Exception ex) {
        Log.e(TAG, "Failed to download exchange rates from " + url, ex);
      }
    }
    return result;
  }
于 2012-04-22T19:28:17.697 回答