Android Volley JSONObjectRequest更改JSON对象顺序

时间:2018-06-11 03:43:18

标签: java android json android-volley jsonobjectrequest

jsonObjectRequest来自这个网址:

https://min-api.cryptocompare.com/data/pricemulti?fsyms=BCC,BTC,DASH,ETH&tsyms=USD

当你去那个网址时,会返回这个json对象:

{"BCC":{"USD":13091},"BTC":{"USD":6783.54},"DASH":{"USD":274.53},"ETH": 
{"USD":531.81}}

然而,jsonObjectRequest返回响应:

{"ETH":{"USD":530.83},"BCC":{"USD":13091},"DASH":{"USD":274.36},"BTC": 
{"USD":6783.94}}

代码:

private void volleyRequest(List<String> coins) {

    String url = "https://min-api.cryptocompare.com/data/pricemulti?fsyms=BCC,BTC,DASH,ETH";

    String currency = "&tsyms=USD";

    url += currency;

    Log.i("URL2", url);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
            (Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.i("Response", response.toString());
                    try {
                        JSONArray keys = response.names();
                        for (int i = 0; i < keys.length(); i++) {
                            String key = keys.getString(i);
                            Double value = response.getJSONObject(key).getDouble("USD");
                            mCoins.add(new Coin(key, value));
                        }

                        mAdapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    mCoins.clear();

    MySingleton.getInstance(getContext()).addToRequestQueue(jsonObjectRequest);
}

日志:

JSON对象请求之前的URL

 I/URL2: https://min-api.cryptocompare.com/data/pricemulti?fsyms=BCC,BTC,DASH,ETH&tsyms=USD

JSONObject响应

 I/Response: {"ETH":{"USD":530.83},"BCC":{"USD":13091},"DASH":{"USD":274.36},"BTC":{"USD":6783.94}}

这只发生在旧版本的android上。 API 19在API 19中不会出现问题。

为什么会发生这种情况以及如何解决这个问题?

0 个答案:

没有答案