I'm having some trouble with an App I'm developing right now that uses this JSON Object: http://api.worldweatheronline.com/premium/v1/marine.ashx?q=-34.8799074,174.7565664&key=3477e975c4d9a8a0eaac2b2c818f4&format=json&tide=yes
Some days it works just fine, fetching the data from the JSON Object without a problem and some days I get this kind of error: JSONException: Index 5 out of range [0..5).
12-04 11:12:30.328 1916-1931/android_kaizen.com.drawerskippermate E/FetchWeatherTask﹕ Index 5 out of range [0..5)
org.json.JSONException: Index 5 out of range [0..5)
at org.json.JSONArray.get(JSONArray.java:263)
at org.json.JSONArray.getJSONObject(JSONArray.java:480)
at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.getWeatherDataFromJson(MainActivity.java:579)
at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.doInBackground(MainActivity.java:754)
at android_kaizen.com.drawerskippermate.MainActivity$FetchWeatherTask.doInBackground(MainActivity.java:318)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask.run(FutureTask.java:234)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
at java.lang.Thread.run(Thread.java:864)
Without even changing a single line of code!! The weird thing is that can see the JSON Object in Web Browser.
Has anyone experienced similar situation and could explain why it happens?
Thx!
Edit: Here is my code:
private DayForecast[] getWeatherDataFromJson(String forecastJsonStr) throws JSONException {
final String JSON_DATA = "data";
final String JSON_WEATHER = "weather";
final String JSON_MAX = "maxtempC";
final String JSON_MIN = "mintempC";
final String JSON_DATE = "date";
final String JSON_ARRAY_HOURLY = "hourly";
final String JSON_ARRAY_ASTRONOMY = "astronomy";
final String JSON_ARRAY_TIDES = "tides";
final int JSON_OBJECT_MORNING = 3;
final int JSON_OBJECT_MIDDAY = 4;
final int JSON_OBJECT_AFTERNOON = 5;
final String JSON_DESCRIPTION = "weatherDesc";
// WeatherCode and description at midday
final String JSON_WEATHER_CODE = "weatherCode";
final String JSON_WEATHER_DESCRIPTION = "weatherDesc";
final String JSON_TEMP = "tempC";
final String JSON_PRECIPITATION = "precipMM";
final String JSON_CLOUD_COVER = "cloudcover";
final String JSON_VISIBILITY = "visibility";
final String JSON_SUNRISE = "sunrise";
final String JSON_SUNSET = "sunset";
final String JSON_PRESSURE = "pressure";
final String JSON_WIND_DIRECTION = "winddir16Point";
final String JSON_WIND_SPEED = "windspeedKmph";
final String JSON_WIND_GUST = "WindGustKmph";
final String JSON_WATER_TEMP = "waterTemp_C";
final String JSON_SWELL_DIRECTION = "swellDir16Point";
final String JSON_SWELL_HEIGHT = "swellHeight_m";
final String JSON_TIDE = "tideTime";
JSONObject forecastJson = new JSONObject(forecastJsonStr);
JSONArray weatherArray = forecastJson.getJSONObject(JSON_DATA).getJSONArray(JSON_WEATHER);
int numDays = weatherArray.length();
mDayForecastArray = new DayForecast[numDays];
for (int i = 0; i < weatherArray.length(); i++) {
String day;
String description;
String highAndLow;
JSONObject dayForecast = weatherArray.getJSONObject(i);
String dateTime = dayForecast.getString(JSON_DATE);
day = getReadableDateString(dateTime);
// Get the right JsonObject for different time of the day
JSONObject morningObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_MORNING);
JSONObject midDayObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_MIDDAY);
JSONObject afternoonObject = dayForecast.getJSONArray(JSON_ARRAY_HOURLY).getJSONObject(JSON_OBJECT_AFTERNOON);
/**
* Common Conditions.
*/
// Parse the two midday values from Json to variables
/**
* Morning Conditions.
*/
description = midDayObject.getJSONArray(JSON_DESCRIPTION).getJSONObject(0).getString("value");
int high = dayForecast.getInt(JSON_MAX);
int low = dayForecast.getInt(JSON_MIN);
highAndLow = formatHighLows(high, low);
DayForecast dayForecastObject = new DayForecast();
dayForecastObject.setmDate(day);
dayForecastObject.setmDescription(description);
dayForecastObject.setmWeatherCode(afternoonObject.getInt(JSON_WEATHER_CODE));
dayForecastObject.setmWeatherDescription(afternoonObject.getJSONArray(JSON_DESCRIPTION).getJSONObject(0).getString("value"));
dayForecastObject.setmTempMax(convertCelsiusToFahrenheit(dayForecast.getInt(JSON_MAX)));
dayForecastObject.setmTempMin(dayForecast.getInt(JSON_MIN));
/**
* Morning Conditions.
*/
dayForecastObject.setmPrecipitationAM(convertmillimetersToInches(morningObject.getInt(JSON_PRECIPITATION)));
dayForecastObject.setmCloudCoverAM(morningObject.getInt(JSON_CLOUD_COVER));
dayForecastObject.setmVisibilityAM(convertKilometersToMiles(morningObject.getInt(JSON_VISIBILITY)));
dayForecastObject.setmSunrise(dayForecast.getJSONArray(JSON_ARRAY_ASTRONOMY).getJSONObject(0).getString(JSON_SUNRISE));
dayForecastObject.setmPressureAM(morningObject.getInt(JSON_PRESSURE));
dayForecastObject.setmWindSpeedAM(convertKilometersToMiles(morningObject.getInt(JSON_WIND_SPEED)));
dayForecastObject.setmWindGustAM(convertKilometersToMiles(morningObject.getInt(JSON_WIND_GUST)));
dayForecastObject.setmWindDirectionAM(morningObject.getString(JSON_WIND_DIRECTION));
dayForecastObject.setmWaterTempAM(convertCelsiusToFahrenheit(morningObject.getInt(JSON_WATER_TEMP)));
dayForecastObject.setmSwellHeightAM(convertMetersToFeet(Float.parseFloat(morningObject.getString(JSON_SWELL_HEIGHT))));
dayForecastObject.setmSwellDirectionAM(morningObject.getString(JSON_SWELL_DIRECTION));
dayForecastObject.setmTide1AM(getReadableTide(dayForecast, 0));
dayForecastObject.setmTide2AM(getReadableTide(dayForecast, 1));
/**
* Afternoon Conditions.
*/
dayForecastObject.setmPrecipitationPM(convertmillimetersToInches(afternoonObject.getInt(JSON_PRECIPITATION)));
dayForecastObject.setmCloudCoverPM(afternoonObject.getInt(JSON_CLOUD_COVER));
dayForecastObject.setmVisibilityPM(convertKilometersToMiles(afternoonObject.getInt(JSON_VISIBILITY)));
dayForecastObject.setmSunset(dayForecast.getJSONArray(JSON_ARRAY_ASTRONOMY).getJSONObject(0).getString(JSON_SUNSET));
dayForecastObject.setmPressurePM(afternoonObject.getInt(JSON_PRESSURE));
dayForecastObject.setmWindSpeedPM(convertKilometersToMiles(afternoonObject.getInt(JSON_WIND_SPEED)));
dayForecastObject.setmWindGustPM(convertKilometersToMiles(afternoonObject.getInt(JSON_WIND_GUST)));
dayForecastObject.setmWindDirectionPM(afternoonObject.getString(JSON_WIND_DIRECTION));
dayForecastObject.setmWaterTempPM(convertCelsiusToFahrenheit(afternoonObject.getInt(JSON_WATER_TEMP)));
dayForecastObject.setmSwellHeightPM(convertMetersToFeet(Float.parseFloat(afternoonObject.getString(JSON_SWELL_HEIGHT))));
dayForecastObject.setmSwellDirectionPM(afternoonObject.getString(JSON_SWELL_DIRECTION));
dayForecastObject.setmTide1PM(getReadableTide(dayForecast, 2));
dayForecastObject.setmTide2PM(getReadableTide(dayForecast, 3));
mDayForecastArray[i] = dayForecastObject;
}
for (DayForecast s : mDayForecastArray) {
Log.v(LOG_TAG, "Forecast entry: " + s.getmDescription() + " / " + String.valueOf(s.getmWeatherCode()));
}
return mDayForecastArray;
}
I´m pretty sure you have an issue similar to: JSONArray Exception : Index 50 out of range [0..50). Is there any limit on JsonArray
The number of objects inside your .json response will change everyday, you have to define bounds (results.length()
):
See this example:
JSONArray results = jsonResponse.getJSONArray("results");
final int numberOfItemsInResp = results.length();
for (int i = 0; i < numberOfItemsInResp; i++){
JSONObject perResult = results.getJSONObject(i);
}