Android 截击请求-如果URL错误,则无响应,如何在截击请求中检测此场景?
您好,我正在尝试从JSON返回的URL下载一些图像,但如果这样的请求URL是错误的,我不会从截击请求中得到任何响应,它只是在那里停留了很长时间。我在下面包含了一个示例代码块。FetchImage方法在循环中被调用,但从存在URL的请求来看,onResponse或onError块中没有响应,我无法检测进程是否已结束Android 截击请求-如果URL错误,则无响应,如何在截击请求中检测此场景?,android,request,android-volley,Android,Request,Android Volley,您好,我正在尝试从JSON返回的URL下载一些图像,但如果这样的请求URL是错误的,我不会从截击请求中得到任何响应,它只是在那里停留了很长时间。我在下面包含了一个示例代码块。FetchImage方法在循环中被调用,但从存在URL的请求来看,onResponse或onError块中没有响应,我无法检测进程是否已结束 final RequestQueue queue = Volley.newRequestQueue(this); progressbar.setVisibility(View.
final RequestQueue queue = Volley.newRequestQueue(this);
progressbar.setVisibility(View.VISIBLE);
String url = Constants.API_BASE_URL
FetchImage(url, queue);
private void FetchImage(String url, RequestQueue queue) {
//get image file name
final String imageFile = utils.getFileNameFromPath(url);
final File imgDIR = new File(systemCardSavePath);
if(!imgDIR.exists()){
imgDIR.mkdirs();
}
ImageRequest imageRequest = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
try{
File file = new File(imgDIR, imageFile);
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileoutputstream = new FileOutputStream(file);
ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
response.compress(Bitmap.CompressFormat.PNG, 100, bytearrayoutputstream);
fileoutputstream.write(bytearrayoutputstream.toByteArray());
fileoutputstream.close();
cardData.put(imageFile, "1");
requestsCounter.decrementAndGet();
if (requestsCounter.get() == 0) {
progressbar.setVisibility(View.INVISIBLE);
}
}catch (FileNotFoundException exFNFE){
exFNFE.printStackTrace();
}catch (IOException exIO){
exIO.printStackTrace();
}
}
}, 0, 0, ImageView.ScaleType.CENTER_CROP, null, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//do error handling
}
});
queue.add(imageRequest);
requestsCounter.incrementAndGet();
}
final RequestQueue=Volley.newRequestQueue(this);
progressbar.setVisibility(View.VISIBLE);
字符串url=Constants.API\u BASE\u url
获取图像(url、队列);
私有void FetchImage(字符串url、请求队列){
//获取图像文件名
最终字符串imageFile=utils.getFileNameFromPath(url);
最终文件imgDIR=新文件(systemCardSavePath);
如果(!imgDIR.exists()){
imgDIR.mkdirs();
}
ImageRequest ImageRequest=新的ImageRequest(url,
新的Response.Listener(){
@凌驾
公共void onResponse(位图响应){
试一试{
File File=新文件(imgDIR、imageFile);
if(file.exists()){
delete();
}
createNewFile();
FileOutputStream FileOutputStream=新的FileOutputStream(文件);
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
compress(Bitmap.CompressFormat.PNG,100,bytearrayoutputstream);
fileoutputstream.write(bytearrayoutputstream.toByteArray());
fileoutputstream.close();
cardData.put(图像文件,“1”);
requestsCenter.decrementAndGet();
if(requestsCenter.get()==0){
progressbar.setVisibility(View.INVISIBLE);
}
}捕获(FileNotFoundException exFNFE){
exFNFE.printStackTrace();
}捕获(IOEXIO异常){
exIO.printStackTrace();
}
}
},0,0,ImageView.ScaleType.CENTER\u裁剪,null,新响应。ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//做错误处理
}
});
添加(imageRequest);
requestsCenter.incrementAndGet();
}
如果url错误或不正确的url格式,或者甚至不存在,那么我们将得到BadUrl异常。然后通过传递一些消息来处理错误。给出正确的url是我们的责任。这是开发人员的错误。这与用户无关。要处理不同类型的错误,我们需要对截击进行正确的错误处理,如下所示(错误处理的顺序也很重要):
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError) {
} else if(error instanceof ClientError) {
//404 error can be further traced under ClientError section
if(((NetworkResponse) ((ClientError)error).networkResponse).statusCode == 404){
}
} else if (error instanceof ServerError) {
} else if (error instanceof AuthFailureError) {
} else if (error instanceof NetworkError) {
} else if (error instanceof ParseError) {
} else if (error instanceof NoConnectionError) {
}
}
}