[ACCEPTED]-Android - Wait for volley response to return-android-volley
A reusable Factory method design pattern solution:
To return or get Volley response from another 12 method, you need to write a Callback functionality, which 11 is all easy using Interfaces
This simple 10 solution is taken from my MVC architecture 9 for Android apps developed with complete 8 reusability and separation of concern concept.
Supposing JSONObject is your 7 response from server
Step 1)
Create an Interface ServerCallback
package xx.xx.xx.utils;
import org.json.JSONObject;
public interface ServerCallback{
void onSuccess(JSONObject result);
}
Step 6 2) Supposing yours Volley server request method is in Controller or any 5 other shared 'context' class do this in 4 yours any Activity
Controller controller = new Controller();
controller.youFunctionForVolleyRequest(email, password, this, loginUrl, new ServerCallback() {
@Override
public void onSuccess(JSONObject response) {
// do stuff here
}
}
);
3) In your Controller class, call 3 ServerCallback function in inResponse() which will execute yours 2 code in Activity only on the response from 1 the server- mission accomplished!
public void youFunctionForVolleyRequest(final String email , final String password ,final Context context , final String URL, final ServerCallback callback)
{
HashMap<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
Log.e("sending json",params.toString());
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
URL, new JSONObject(params), new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response); // call call back function here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("Volley error json object ", "Error: " + error.getMessage());
}
}){
@Override
public String getBodyContentType()
{
return "application/json";
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
You should probably not return the link 15 in your method.
Volley is doing asynchronous tasks, it 14 means that you can't know when the answer 13 will arrive from your webservice, it could 12 be 10sec or 10min.
If you need the string 11 in your function you should probably create 10 a method and call it when you have the result.
Here 9 is an example:
I guess this is what you have
public void getTheLinkAndCallDoSomething(){
String link = getLink();
doSomethingWithTheLink(link);
}
This 8 would work if getLink()
answers in a synchronous 7 way. Volley is not in this case.
And this 6 is what you can do with Volley:
public void getTheLinkAndCallDoSomething(){
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET,
url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
shortenURL = response.getString("url");
doSomethingWithTheLink(shortenURL);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
}
In this case, you 5 call the WS to get your url. And as soon 4 as the result is known, you call doSomethingWithTheLink()
Whatever, if 3 you really do want to be synchronous you can look at 2 this post : Wait for result of Async Volley request and return it
Also, be aware that waiting for an answer could freeze your app UI and I guess 1 that is not what you want.
you cannot return anydata in your getLink()
method.
Use 1 your code as
String shortenURL = ""; // activity global variable
public void getLink() {
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
shortenURL = response.getString("url");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
}
});
queue.add(jsObjRequest);
}
You can also see more info at http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/
You can do this using the future concept
RequestFuture<JSONObject> jsonObjectRequestFuture = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(ServiceUrl.BFF_BASE_URL + path, (JSONObject) postBody, jsonObjectRequestFuture, jsonObjectRequestFuture);
requestQueue.add(request);
JSONObject jsonObject = jsonObjectRequestFuture.get(30, TimeUnit.SECONDS);
0
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.