I have problem running this method, it doesn't execute in order.
private Double balance;
public double getBalance(Address address){
balance=0.0;
String url = urlAPI+address.toString();
RequestQueue reqQueue;
reqQueue = Volley.newRequestQueue(this);
//Comment #1
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//Comment #2
if (response.get("data").toString() != "null") {
balance = Double.parseDouble(response.get("data").toString()) / 100000000;
} else {
balance = 100.00000001;
}
;
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}
);
//Comment #3
reqQueue.add(jsonObjReq);
return balance;
}
Its execute order goes Comment #1 -> #3 -> #2. It even finishes tasks outside the method before return to finish #2. This causes incorrectly return value.
Any help would be much appreciated.