I'm trying to get array of int values from JSON, however something strange is happening and i get different output when i read the same array on two places in the code.
Does anyone know the reason behind this behaviour?
public LineGraphSeries<DataPoint> fillGraph(String city, int numberOfDays){
int data[] = new int[numberOfDays*8];
DataPoint[] temperatures = new DataPoint[numberOfDays*8];
LineGraphSeries<DataPoint> graph;
String url = "http://api.worldweatheronline.com/free/v2/weather.ashx?q=" + city + "&format=json&num_of_days=" + numberOfDays + "&cc=no&fx24=no&show_comments=no&tp=3&key=e74975c820b1f6506bd6b9fdea5a5";
JSONObject dataZNetu;
JSONArray dataArray;
JSONObject dataHourly;
JSONArray dataHourlyArray;
try {
dataZNetu = requestWebService(url).getJSONObject("data");
dataArray = dataZNetu.getJSONArray("weather");
for(int i = 0; i<dataArray.length(); i++){
dataHourly = dataArray.getJSONObject(i);
dataHourlyArray = dataHourly.getJSONArray("hourly");
for(int j = 0; j<dataHourlyArray.length(); j++){
data[i*j] = dataHourlyArray.getJSONObject(j).getInt("FeelsLikeC");
//temperatures[i*j] = new DataPoint(i*j,data[i*j]);
Log.v("dataCorrect" + i,String.valueOf(data[i*j])); //Correct values
}
}
}
catch (Exception e) {
e.printStackTrace();
}
for(int i = 0;i<data.length;i++){
Log.v("dataBroken" + i/8,String.valueOf(data[i])); //Broken values
}
graph = new LineGraphSeries<DataPoint>(temperatures);
return graph;
}
Log:
05-15 02:05:50.609 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 6
05-15 02:05:50.609 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 5
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 12
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 18
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 19
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 20
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 16
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect0﹕ 14
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 10
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 7
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 16
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 20
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 24
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 24
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 18
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect1﹕ 15
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 15
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 16
05-15 02:05:50.619 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 17
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 17
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 14
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataCorrect2﹕ 11
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 11
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 7
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 11
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 20
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 15
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 24
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 16
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken0﹕ 15
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 17
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 17
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 14
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 11
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken1﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
05-15 02:05:50.629 13125-13125/com.example.tomus.weatherdresser V/dataBroken2﹕ 0
data[i*j]??? Are you sure? Maybe you mean something likedata[(i*8)+j]? Whatever - your indexing arithmetic is completely different between the "Correct: and "Broken" examples :(