从openweathermap解析JSON时获取NegativeArraySizeException

时间:2014-11-29 17:29:41

标签: android json weather-api openweathermap

我正在尝试使用Async类获取数据,但是我没有获取数据,而是获得了NegativeArraySizeException。虽然所有其他事情都是正确的,但我不知道自己犯了什么错误。

以下是我用于获取数据的代码:

private class FetchData extends AsyncTask<Object, Void, String>
{

    @Override
    protected String doInBackground(Object... arg0) {

        int responseCode = -1;
        try 
        {
            URL apiURL = new URL("http://api.openweathermap.org/data/2.5/weather?q=london&mode=json&units=metric");
            HttpURLConnection connection = (HttpURLConnection) apiURL.openConnection();
            connection.connect();

            responseCode = connection.getResponseCode();
            if(responseCode == HttpURLConnection.HTTP_OK)
            {
                InputStream inputStream = connection.getInputStream();
                Reader reader = new InputStreamReader(inputStream);
                int contentLength = connection.getContentLength();
                char[] charArray = new char[contentLength];
                reader.read(charArray);
                String responseData = new String(charArray);

                JSONObject jsonResponse = new JSONObject(responseData);

                JSONArray weather_icon  = jsonResponse.getJSONArray("weather");
                 JSONObject obj_icon = weather_icon.getJSONObject(0);
                 String icon = obj_icon.getString("icon");
                 String status = obj_icon.getString("description");

                 String location = jsonResponse.getString("name");

                 JSONObject main  = jsonResponse.getJSONObject("main");
                 String tempreature = main.getString("temp");

                 String humidity = main.getString("humidity");
                 String pressure = main.getString("pressure");


                 pTempreature = tempreature;
                 pHumidity = humidity;
                 pPressure = pressure;
                 pLocation = location;
                 pStatus = status;
                 pIcon = icon;


                 Intent i = new Intent(MainActivity.this, DisplayData.class);
                 i.putExtra("tem", tempreature);
                 i.putExtra("ico", icon);
                 i.putExtra("hum", humidity);
                 i.putExtra("pre", pressure);
                 i.putExtra("stat", status);
                 i.putExtra("loc", location);
                 startActivity(i);

            }




        }
        catch (MalformedURLException e)
        {
            Log.e("log_tag","Error In URL"+e.toString());
        }
        catch (IOException e)
        {
            Log.e("log_tag","Error In URL"+e.toString());
        }
        catch (Exception e)
        {
            Log.e("log_tag","Error In URL"+e.toString());
        }

        return "Code :"+responseCode;
    }
}

1 个答案:

答案 0 :(得分:0)

我没有运行您的代码,但可能您遇到了问题:

char[] charArray = new char[contentLength];

当我查看此page内容时,没有为您的网址设置长度,因此您在contentLength中获得-1。试试这段代码来获取你的内容:

StringBuffer buffer = new StringBuffer();

            if (inputStream == null) {
                // Nothing to do.
                return null;
            }
            reader = new BufferedReader(new InputStreamReader(inputStream));

            String line;
            while ((line = reader.readLine()) != null) {
                // Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
                // But it does make debugging a *lot* easier if you print out the completed
                // buffer for debugging.
                buffer.append(line + "\n");
            }

            if (buffer.length() == 0) {
                // Stream was empty.  No point in parsing.
                return null;
            }
            forecastJsonStr = buffer.toString();