0

jsonObjectRequest 来自此网址:

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

当您转到该 url 时,它会返回此 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 25 不会出现问题,但 API 19 会出现问题。

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

4

0 回答 0