Error handling 在截击中处理请求未来调用的错误

Error handling 在截击中处理请求未来调用的错误,error-handling,android-volley,synchronous,Error Handling,Android Volley,Synchronous,我正在使用Volley库提供的RequestFuture进行同步api调用。 如果状态代码为4xx/500,我需要处理错误响应 try { JSONObject response = future.get(); } catch (InterruptedException e) { // exception handling } catch (ExecutionException e) { // exception handling } 现在,错误被ExecutionExce

我正在使用Volley库提供的RequestFuture进行同步api调用。 如果状态代码为4xx/500,我需要处理错误响应

    try {
  JSONObject response = future.get();
} catch (InterruptedException e) {
  // exception handling
} catch (ExecutionException e) {
  // exception handling
}
现在,错误被ExecutionExceptioncatch子句捕获。如何从该错误中获取网络响应


如何覆盖catch子句中的onErrorListener

试着从截击中获取错误。另外,在执行未来的请求时,还需要注意,您应该使用带有超时的get,这样您就不会永远等待

try
{
   JSONObject response = future.get(30,TimeUnit.SECONDS);
}
catch(InterruptedException | ExecutionException ex)
{
   //check to see if the throwable in an instance of the volley error
   if(ex.getCause() instanceof VolleyError)
   {
       //grab the volley error from the throwable and cast it back
       VolleyError volleyError = (VolleyError)ex.getCause();
      //now just grab the network response like normal
      NetworkResponse networkResponse = volleyError.networkResponse;
   }                          
}
catch(TimeoutException te)
{
   Log.e(TAG,"Timeout occurred when waiting for response");
}