凌空同步请求永远阻止

Moh*_*ush 6 android android-volley

我想使用volley库进行同步请求,我使用了以下代码:

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

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

        try {

            Long response = future.get();
Run Code Online (Sandbox Code Playgroud)

但代码在这里永远阻止:

Long response = future.get();
Run Code Online (Sandbox Code Playgroud)

这是我的自定义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));
            }
    }
Run Code Online (Sandbox Code Playgroud)

我调试了凌空代码,它在NetworkDispatcher中停止

//从队列中获取请求.

   request = mQueue.take();
Run Code Online (Sandbox Code Playgroud)

fli*_*r83 10

这个问题很老,但是没有任何回应.

我会尝试为其他人写一个响应有同样的问题,我相信这里的问题是你从主线程运行未来,并且默认用户线程这个线程执行响应.

Future在他的代码中等待,如果你从主线程运行它你正在停止这个线程,并且响应处理程序线程永远不会执行它,对于这个问题,这仍然在等待.