Android - Wait For Volley Response For Continue
I build an app, that it load markers in a map, the markers I get it for volley JSON file, but I need that first to load volley, and after continue executing the code, because the o
Solution 1:
You would need to implement a call back for the volley response. A very simple way to do this would be the following.
Step 1: Create this interface
publicinterfaceVolleyCallBack {
voidonSuccess();
}
Step 2: change your getMarkers()
method to this:
publicvoidgetMarkers(final VolleyCallBack callBack){
requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = newJsonObjectRequest(Request.Method.POST, url, newResponse.Listener<JSONObject>() {
@OverridepublicvoidonResponse(JSONObject response) {
try {
JSONArray saagTracker = response.getJSONArray("saagMRK");
for (int i = 0; i < saagTracker.length(); i++) {
JSONObjectobject = saagTracker.getJSONObject(i);
title = object.getString(TITLE);
snnipet = object.getString(SNNIP);
latLng = newLatLng(Double.parseDouble(object.getString(LAT)), Double.parseDouble(object.getString(LNG)));
coor = coor + "|" + object.getString(LAT) + "," + object.getString(LNG); // Menambah data marker untuk di tampilkan ke google mapaddMarker(latLng, title, snnipet);
callback.onSuccess();
}
} catch (JSONException e) {
e.printStackTrace();
}
//JSONArray array = new JSONArray(response.body().string());
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Ocurrio un error", Toast.LENGTH_LONG).show();
}
});
requestQueue.add(jsonObjectRequest);
}
Step 3: Call your getMarkers()
method like this:
getMarkers(newVolleyCallBack() {
@OverridepublicvoidonSuccess() {
// this is where you will call the geofire, here you have the response from the volley.
geoQuery = geofire.queryAtLocation(newGeoLocation(latLng.latitude, latLng.longitude),0.1f);
});
Post a Comment for "Android - Wait For Volley Response For Continue"