I am developing a weather app for that I am using dark sky API in which I want to know the weather status of a bunch of locations which I have stored in ArrayList<LatLng>
.
I am using OKHttp
to parse the JSON data from API, so I tried to loop the whole fetching process inside for loop but it doesn't give the desired output.
private void beginTask(ArrayList<LatLng> arrayLis) {
//arraylis contains list of locations(LatLng)
m = 0;
startTask = true;
for (int i = 0;i<arrayLis.size();i++) {
double latitude = ((LatLng)arrayLis.get(i)).latitude;
double longitude = ((LatLng)arrayLis.get(i)).longitude;
String url = "https://api.darksky.net/forecast/APIKEY/"
+latitude+","+longitude+"?units=si";
LatLng mylatlng = new LatLng(latitude,longitude);
startProcess(url,mylatlng);
Log.i("GGGTT",""+latitude+", "+longitude);
}
}
private void startProcess(String myurl, final LatLng myLatlng){
OkHttpClient httpClient = new OkHttpClient();
Request request = new Request.Builder()
.url(myurl)
.build();
Call call = httpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String data = response.body().string();
Log.i("DATASS",data);
if (response.isSuccessful()){
try {
getCurrentDetails(data,myLatlng);
} catch (JSONException e){
e.printStackTrace();
}
}
}
});
}
private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException{
JSONObject main = new JSONObject(data);
double la = main.getDouble("latitude");
double lon = main.getDouble("longitude");
JSONObject currently = main.getJSONObject("currently");
String summary = currently.getString("summary");
double temperature = currently.getDouble("temperature");
String icon = currently.getString("icon");
LatLng latLngo = new LatLng(la,lon);
ter.add(latLngo);
weatherInfo.add(icon);
// Output here is not in the same order that I have passed
Log.i("LETSCHECK",""+la+", "+lon +icon);
}
I am passing the values as:
But the output is not in the same order inside the getCurrentDetails
method
I think the method is not waiting before the previous loop gets completed.
Are there any solutions for getting the weather status of all locations stored in ArrayList
without changing its order?
EDIT
Hi, I have gone through this method to fetch data in order and its working fine, thanks but one more problem is that I was expecting to show data 4 times as there are four LatLngs in ArrayList and it's working fine, but when I try to read the fetching data that I have stored in another array it only shows 2 items rather than 4.
private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException{
JSONObject main = new JSONObject(data);
double la = main.getDouble("latitude");
double lon = main.getDouble("longitude");
JSONObject currently = main.getJSONObject("currently");
String summary = currently.getString("summary");
double temperature = currently.getDouble("temperature");
String icon = currently.getString("icon");
//Log.i("LETSCHECK",""+la+", "+lon +icon+",k");
loopi++;
Log.i("LETSCHECK",""+loopi);
if (loopi < arrayList.size()) {
getItAgain();
} else if (loopi == arrayList.size()){
for (String l:weatherInfo){
Log.i("LETSCHECK",l);
//expected 4 items to show but its showing only 2 items
}
}
Log.i("LETSCHECK",""+la+", "+lon +icon);
weatherInfo.add(icon);
}
private void getItAgain() {
double latitude = ((LatLng)arrayList.get(loopi)).latitude;
double longitude = ((LatLng)arrayList.get(loopi)).longitude;
String url = "https://api.darksky.net/forecast/74d8feeda5ecf5ee6667d034778b239d/"
+latitude+","+longitude+"?units=si";
LatLng mylatlng = new LatLng(latitude,longitude);
startProcess(url,mylatlng);
}
The network calls are asynchronous and this the expected behavior that you are having here. If you want to get them sorted as you have passed them, then you might need to have a mechanism of waiting for the network call of the previous one to be completed and then again call the next one. However, this might increase the delay of waiting for the response from the server, as you are not fetching multiple responses from the server concurrently.
If you want to wait for the response to maintain the sorted manner, you might just need to do the following.
// Declare a global variable of the list of your locations.
ArrayList<LatLng> arrayLis;
int i = 0;
// Just start the first task without the loop.
double latitude = ((LatLng)arrayLis.get(i)).latitude;
double longitude = ((LatLng)arrayLis.get(i)).longitude;
String url = "https://api.darksky.net/forecast/APIKEY/"
+latitude+","+longitude+"?units=si";
LatLng mylatlng = new LatLng(latitude,longitude);
startProcess(url,mylatlng);
Now in the getCurrentDetails
function, you might just have to do the following.
private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException{
JSONObject main = new JSONObject(data);
double la = main.getDouble("latitude");
double lon = main.getDouble("longitude");
JSONObject currently = main.getJSONObject("currently");
String summary = currently.getString("summary");
double temperature = currently.getDouble("temperature");
String icon = currently.getString("icon");
LatLng latLngo = new LatLng(la,lon);
ter.add(latLngo);
weatherInfo.add(icon);
// Now initiate the next call
i++;
if(i < arrayLis.size()) getItAgain();
Log.i("LETSCHECK",""+la+", "+lon +icon);
}
public void getItAgain() {
// Just start the first task without the loop.
double latitude = ((LatLng)arrayLis.get(i)).latitude;
double longitude = ((LatLng)arrayLis.get(i)).longitude;
String url = "https://api.darksky.net/forecast/APIKEY/"
+latitude+","+longitude+"?units=si";
LatLng mylatlng = new LatLng(latitude,longitude);
startProcess(url,mylatlng);
}
Update
I do not understand why it is showing 2 instead of 4 results. However, I think you need to modify the code as follows.
private void getCurrentDetails(String data,LatLng myLatlng) throws JSONException {
JSONObject main = new JSONObject(data);
double la = main.getDouble("latitude");
double lon = main.getDouble("longitude");
JSONObject currently = main.getJSONObject("currently");
String summary = currently.getString("summary");
double temperature = currently.getDouble("temperature");
String icon = currently.getString("icon");
// Move this upto here
weatherInfo.add(icon);
loopi++;
Log.i("LETSCHECK",""+loopi);
if (loopi < arrayList.size()) {
getItAgain();
} else {
for (String l:weatherInfo) {
Log.i("LETSCHECK",l);
//expected 4 items to show but its showing only 2 items
}
}
}