volley synchronous request blocks forever
Asked Answered
H

3

6

I want to make synchronous request using volley library and I used the following code :

RequestFuture<Long> future = RequestFuture.newFuture();

        AuthenticatedJsonRequest request = new AuthenticatedJsonRequest(Method.GET,ServiceUrl,null,future,future);
        requestQueue.add(request);

        try {

            Long response = future.get();

but the code is blocking forever here :

Long response = future.get();

and this is my custom JsonRequest

public class AuthenticatedJsonRequest extends JsonRequest<Long> {



    public AuthenticatedJsonRequest(int method, String url, String requestBody, Listener<Long> listener,
            ErrorListener errorListener) {
        super(method, url, requestBody, listener, errorListener);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = new HashMap<String, String>();
        String creds = String.format("%s:%s", RestClient.UserName, RestClient.Password);
        String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
        headers.put("Authorization", auth);
        return headers;
    }



    @Override
    protected Response<Long> parseNetworkResponse(NetworkResponse response) {
          try {

               String jsonString =
                    new String(response.data, HttpHeaderParser.parseCharset(response.headers));
                return Response.success(Long.valueOf(jsonString),
                        HttpHeaderParser.parseCacheHeaders(response));
            } catch (UnsupportedEncodingException e) {
                return Response.error(new ParseError(e));
            }
    }

I debugged volley code and it is stopping in NetworkDispatcher

// Take a request from the queue.

   request = mQueue.take();
Hallah answered 19/1, 2014 at 22:11 Comment(1)
the new version of volley don't cause problemHallah
O
10

This question is old, but this doesn't have any response.

I will try write a response for other people have the same problem, I believe that the problem here is that you are running the future from the main thread, and volley user this thread by default for execute the responses.

Future makes a wait in his code, if you run it from main thread you are stopping this thread, and the response handler thread never execute it, an for this problem this is still waiting forever.

Outmoded answered 7/3, 2014 at 11:7 Comment(0)
C
1

Three things:

  1. Use future.get() method with timeouts, like this:

future.get(30, TimeUnit.SECONDS);

  1. Make sure that you are not calling future.get() before adding the request to the queue.
  2. It won't work out on main thread. Try it in service instead. I don't see a reason you would need it on main thread.

Hope it helps!

Claypoole answered 24/2, 2015 at 0:34 Comment(0)
H
0

The problem is you dont use a timeout for your get(). Use

future.get(20, TimeUnit.SECONDS);

and catch the error.

(And of course, don't use it in ui thread)

Hilten answered 11/9, 2014 at 8:42 Comment(1)
20 seconds ? Are u sure ?Camarilla

© 2022 - 2024 — McMap. All rights reserved.