无法从服务器获取JSON文件

时间:2017-08-19 06:23:12

标签: java android json

我想将以下JSON数据库解析到我的Android应用程序

[
  {
    "name": "Bitcoin",
    "symbol": "BTC",
    "price_usd": 4075.51,
    "price_eur": 3468.74399569,
    "price_btc": 1,
    "volume_eur": 2212237015.99,
    "market_cap_usd": 67303991018,
    "percent_change_1h": -1.33,
    "percent_change_24h": -5.7,
    "percent_change_7d": 14.06
  },
  {
    "name": "district0x",
    "symbol": "DNT",
    "price_usd": 0.155491,
    "price_eur": 0.1323413444,
    "price_btc": 0.00003828,
    "volume_eur": 4358137.81712,
    "market_cap_usd": 93294600,
    "percent_change_1h": -2.65,
    "percent_change_24h": 17.31,
    "percent_change_7d": 91.52
  },
  {
    "name": "Ethereum",
    "symbol": "ETH",
    "price_usd": 295.651,
    "price_eur": 251.634183469,
    "price_btc": 0.0727828,
    "volume_eur": 735525975.253,
    "market_cap_usd": 27812727380,
    "percent_change_1h": -0.65,
    "percent_change_24h": -2.57,
    "percent_change_7d": -2.83
  }
]

使用以下代码:

protected Void doInBackground(Void... arg0) {
        databasehandler sh = new databasehandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url);

        Log.e(TAG, "Response from url: " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONArray jsonArray = new JSONArray(jsonStr);

                // Getting JSON Array node
                JSONArray currencies = jsonArray.getJSONArray(0);

                // looping through currencies
                for (int i = 0; i < currencies.length(); i++) {
                    JSONObject c = currencies.getJSONObject(i);

                    String name = c.getString("name");
                    String symbol = c.getString("symbol");
                    String price_usd = c.getString("price_usd");
                    String price_eur = c.getString("price_eur");
                    String price_btc = c.getString("price_btc");
                    String volume_eur = c.getString("volume_eur");
                    String market_cap_usd = c.getString("market_cap_usd");
                    String percent_change_1h = c.getString("percent_change_1h");
                    String percent_change_24h = c.getString("percent_change_24h");
                    String percent_change_7d = c.getString("percent_change_7d");


                    // tmp hash map for single contact
                    HashMap<String, String> currency = new HashMap<>();

                    // adding each child node to HashMap key => value

                    currency.put("name", name);
                    currency.put("symbol", symbol);

                    // adding currency to currencyList
                    currencyList.add(currency);
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG)
                                .show();
                    }
                });

            }

}

不知怎的,我无法从服务器获取这些数据,我的LogCat日志会说出以下内容:

  

08-15 13:49:07.751 25055-25055 /? E / PGA:PgaSocketInitClientPgaIpc:   ioctl()失败:ret -1,errno 22 08-15 13:49:07.761 25055-25055 /?   I / PGA:PgaSocketInitPgaIpc:打开/ dev / bstpgaipc:fd = 14 08-15   13:49:07.761 25055-25055 /? I / PGA:尝试创建新的SOCKET   connectionn pid = 25055,tid = 25055

编辑:

这是Databasehandler类

public class databasehandler {
    private static final String TAG = databasehandler.class.getSimpleName();

    public String makeServiceCall(String reqUrl) {
        String response = null;
        try {
            URL url = new URL(reqUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            // read the response
            InputStream in = new BufferedInputStream(conn.getInputStream());
            response = convertStreamToString(in);
        } catch (MalformedURLException e) {
            Log.e(TAG, "MalformedURLException: " + e.getMessage());
        } catch (ProtocolException e) {
            Log.e(TAG, "ProtocolException: " + e.getMessage());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.getMessage());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
        return response;
    }

    private String convertStreamToString(InputStream is) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line).append('\n');
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }

}

1 个答案:

答案 0 :(得分:0)

删除此行

 JSONArray currencies = jsonArray.getJSONArray(0);

并在

中使用jsonArray
for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject c = jsonArray.getJSONObject(i);
}