how to wait the volley response to finish it's work inside intentservice?

前端 未结 2 1231
野性不改
野性不改 2021-01-07 03:43

Working with intentservice to get the data of 7 Rss Feed links with using \" Google Volley \" in the background and use ResultReceiver to get the result , But I can\'t confi

2条回答
  •  攒了一身酷
    2021-01-07 04:43

    This question was answered in the official Volley Google Group.

    Wrap the request in a RequestFuture to make a blocking call using RequestFuture#newFuture(...);

    You can find sample code in the Volley source code:

    RequestFuture future = RequestFuture.newFuture();
    MyRequest request = new MyRequest(URL, future, future);
    
    // If you want to be able to cancel the request:
    future.setRequest(requestQueue.add(request));
    
    // Otherwise:
    requestQueue.add(request);
    
    try {
      JSONObject response = future.get();
      // do something with response
    } catch (InterruptedException e) {
      // handle the error
    } catch (ExecutionException e) {
      // handle the error
    }
    

    Be sure to use a timeout in future.get(...) or else your thread will lock.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题