Amara Gul
Amara Gul

Reputation: 93

Synchronous POST method with Volley library

I am sending pin number to my local server using Volley APIs (for completing phone number registration process) as mentioned below

final HashMap<String, String> params = new HashMap<String, String>();
params.put("PHONE_NUMBER",1234567890);
params.put("PIN", 9999);

addRequestToQueue("http://"127.0.0.1:5000"/registerClient", params);


public void addRequestToQueue(String url, final HashMap<String, String> params)
{

JsonObjectRequest jsonRequest = new JsonObjectRequest (url, new JSONObject(params), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) {VolleyLog.v("Response from server: %s", response.toString());}},new Response.ErrorListener() {@Override public void onErrorResponse(VolleyError error) {VolleyLog.e("Error:", error.toString());}});


mRequestQueue.add(jsonRequest);

}

This is working fine and storing information on server but i am unable to get the response code/string from server. Probably it is because of Asynchronous call...

would someone please help me to resolve the issue, i need to get the response string/code from server so that i can verify that user properly registered.

Upvotes: 0

Views: 2842

Answers (1)

Ivan
Ivan

Reputation: 978

Try this

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(Method.POST, url,  new JSONObject(params), future, future)
mRequestQueue.add(request);

try {
    JSONObject response = future.get();
      ... do something
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}

Upvotes: 2

Related Questions