Implementing my LoginActivity.java, I setup another file+class
called AuthClient
which uses Volley heavily. UserLoginTask
needs to return a Boolean
from its doInBackground
so that the remote call to login succeeds/fails.
So following Can I do a synchronous request with volley?, I setup:
void login(final HashMap<String, String> data,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
JsonObjectRequest req = new JsonObjectRequest(
api_prefix + "/auth", new JSONObject(data), listener, errorListener
);
queue.add(req); // RequestQueue
}
boolean loginResponseHandler(RequestFuture<VolleyError> err_future,
RequestFuture<JSONObject> future) {
try {
VolleyError err_response = err_future.get();
JSONObject response = future.get();
System.out.println("response.toString() = " + response.toString());
return true;
} catch (InterruptedException | ExecutionException e) {
System.err.println("e = " + e.toString());
return false;
} catch (Exception e) { // Even tried this!
System.err.println("Exception::e = " + e.toString());
throw e;
}
}
@Override
protected Boolean doInBackground(Void... params) {
RequestFuture<JSONObject> future = RequestFuture.newFuture();
RequestFuture<VolleyError> err_future = RequestFuture.newFuture();
login(/*omitted for brevity*/, err_future, future);
return loginResponseHandler(err_future, future);
} // Actual code has login/loginResponseHandler in separate class+file, and more^
But looking at the debug log, I get the error (from somewhere else, not where I'm debugging):
08-24 23:17:13.228 7255-7288 E/Volley: [177] BasicNetwork.performRequest: Unexpected response code 400 for http://192.168.5.3:3003/v1/api/auth
How am I meant to handle errors? - Also is there a reference scaffold to work from?