Abh*_*dha 2 error-handling synchronous android-volley
我正在使用Volley库提供的RequestFuture进行同步 api调用。如果状态代码为4xx / 500,我需要处理错误响应。
try {
JSONObject response = future.get();
} catch (InterruptedException e) {
// exception handling
} catch (ExecutionException e) {
// exception handling
}
Run Code Online (Sandbox Code Playgroud)
现在,该错误已由ExecutionException catch子句捕获。如何从此错误获取NetworkResponse。
如何在catch子句中覆盖onErrorListener。
小智 5
尝试此操作以解决凌空抽烟的错误。在执行将来的请求时,也请注意,您应该使用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");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1087 次 |
| 最近记录: |