0

i am trying to implement login using volley like below:

public static JSONObject register(RequestQueue requestQueue, String url, HashMap<String, String> params) {

    RequestFuture<JSONObject> requestFuture = RequestFuture.newFuture();
    CustomRequest customRequest = new CustomRequest(Request.Method.POST,url,params,requestFuture,requestFuture);
    JSONObject response = null;

    requestQueue.add(customRequest);
    try {
        response = requestFuture.get(30000, TimeUnit.MILLISECONDS);
    } catch (InterruptedException e) {
        L.m(e + "");
    } catch (ExecutionException e) {
        L.m(e + "");
    } catch (TimeoutException e) {
        L.m(e + "");
    }
    return response;
}

But whenever i send request i get null value as response. the web api call works, it creates the user but i can't get json result in case of an error like duplicate username etc.

parameters are :

url: http://localhost:13480/api/Account/Register Hashmap: (as json):

{ "Email": "admin@admin", "Password": "Password12!", "ConfirmPassword": "Password12!" }

and the custom request is here: Volley - POST/GET parameters So i can't figure out what is wrong. Any helps?

Community
  • 1
  • 1
hibbault
  • 37
  • 5

2 Answers2

0

Try using this instead

JsonObjectRequest jr = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
      // handle the response here 
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {

    }
});

    requestQueue.add(jr);
Andy Joyce
  • 2,802
  • 3
  • 15
  • 24
-1

TL,DR. This statement return response; is wrong.

You are trying to return your response before even getting it (async call).

The fact is that volley does the request asynchronously so your call will end its execution before the request is serverd that's why the response you are getting is null.

Here is subset of a class I wrote that might help you understand how to use Volley properly:

public class SubsetClass {

    private Context context;
    String serverURL;

    public SubsetClass(Context context) {
        this.context = context;
        serverURL = context.getResources().getString(R.string.serverURL);
    }

    private void genericPOSTRequest(Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
        GenericRequest gen = new GenericRequest();
        gen.makePOSTRequest(parameters, responseListener, errorListener);
    }

    private void genericGETRequest(Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
        GenericRequest gen = new GenericRequest();
        gen.makeGETRequest(parameters, responseListener, errorListener);
    }

    private class GenericRequest {

        private void makePOSTRequest(final Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
            RequestQueue queue = Volley.newRequestQueue(context);
            StringRequest stringRequest = new StringRequest(Request.Method.POST, serverURL,
                    responseListener, errorListener) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return parameters;
                }
            };
            queue.add(stringRequest);
        }

        private void makeGETRequest(final Map<String, String> parameters, Response.Listener<String> responseListener, Response.ErrorListener errorListener) {
            RequestQueue queue = Volley.newRequestQueue(context);
            StringRequest stringRequest = new StringRequest(Request.Method.GET, serverURL,
                    responseListener, errorListener) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    return parameters;
                }
            };
            queue.add(stringRequest);
        }
    }
}

Related question

Volley - POST/GET parameters

  • RequestFuture is for making synchronous call so answer isn't correct – Manish Jun 17 '19 at 12:28
  • @Manish I haven't used volley for a couple of years now, but I believe requestQueue.add(customRequest); will schedule the the request to be later executed... – Anis LOUNIS aka AnixPasBesoin Jun 17 '19 at 15:38
  • you are right that requestQueue.add(customRequest); will do it for future but in the asked query RequestFuture has been used which it blocks further execution and response is returned synchronously. response = requestFuture.get(30000, TimeUnit.MILLISECONDS); returns response synchronously. – Manish Jun 18 '19 at 09:17