Error handling in Volley (with futures)?

Implementing my LoginActivity.java , I am installing another + file class, called AuthClientone that uses Volley heavily. UserLoginTaskmust be returned Booleanfrom doInBackgroundso that the remote login call succeeds.

So, following Can I execute 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 an error (from somewhere else, and not where I am debugging):

08-24 23: 17: 13.228 7255-7288 E / Volley: [177] BasicNetwork.performRequest: Unexpected 400 response code for http://192.168.5.3 {003/v1/api/auth

How should I handle errors? . Also there is a reference scaffold for work?

+4
2

VolleyError err_response = err_future.get();

, , - cast.

2 , RequestFuture , .

, .

future.get()

. , Execution, VolleyError, , . , , - VolleyError catch.

try {
    JSONObject response = future.get();
    System.out.println("response.toString() = " + response.toString());
    return true;
} catch (InterruptedException | ExecutionException e) {
    if (VolleyError.class.isAssignableFrom(e.getCause().getClass())) {
        VolleyError ve = (VolleyError) e.getCause();
        System.err.println("ve = " + ve.toString());
        if (ve.networkResponse != null) {
            System.err.println("ve.networkResponse = " +
                               ve.networkResponse.toString());
            System.err.println("ve.networkResponse.statusCode = " +
                               ve.networkResponse.statusCode);
            System.err.println("ve.networkResponse.data = " +
                               new String(ve.networkResponse.data));
            return false;
        }
    }
}

" ", , . :

3 : / , , java /, .

, , .

  • .

5xx , , , .

, , . rxJava - , , , , , .

jus ( ) rx-jus RxQueueHub rxHub, .

+4

, , :

if (err_future != null) throw err_future.get();

try loginResponseHandler, :

VolleyError err_response = err_future.get();

, .

EDIT: ? ? : com.android.volley.NetworkResponse

0

Source: https://habr.com/ru/post/1652340/


All Articles