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;
}
}