
在shouldInterceptRequest()中,我打开连接,添加标题,然后在WebResourceResponse中返回打开的流.
我不清楚如果初始打开连接失败,应该如何处理IOException.
final Map<String,String> extraheaders = getExtraheaders(intent);webvIEw.setWebVIEwClIEnt(new WebVIEwClIEnt() { @OverrIDe public WebResourceResponse shouldInterceptRequest(WebVIEw vIEw,WebResourceRequest request) { final Uri uri = request.getUrl(); try { URL url = new URL(uri.toString()); URLConnection con = url.openConnection(); for (Map.Entry<String,String> h : extraheaders.entrySet()) { con.addRequestProperty(h.getKey(),h.getValue()); } final String ContentType = con.getContentType().split(";")[0]; final String enCoding = con.getContentEnCoding(); return new WebResourceResponse(ContentType,enCoding,con.getinputStream()); } catch (IOException e) { // what should we do Now? e.printstacktrace(); } return super.shouldInterceptRequest(vIEw,request); }}); 我不能让它没有被捕获,因为它是一个已检查的异常,并且不是shouldInterceptRequest()签名的一部分.
我无法将其包装在未经检查的异常中,因为WebVIEw会将其删除并杀死应用程序.
如果我捕获并忽略该异常,并且默认为super方法(它只返回null),那么WebVIEw将继续其默认行为并尝试发送请求(没有我的额外标题).这是不可取的,因为WebVIEw自己的连接尝试可能实际上成功,并且丢失的标题将导致更多的问题.
似乎没有办法表明拦截失败,请求应该中止.
这里最好的事情是什么?
我试图返回模拟失败响应,但这不会被视为错误. WebVIEw显示包含响应内容的页面(来自异常的错误消息),并且不调用WebVIEwClIEnt的onReceivedError()或onReceivedhttpError()回调.
} catch (IOException e) { inputStream is = new ByteArrayinputStream(e.getMessage().getBytes()); return new WebResourceResponse("text/plain","UTF-8",500,"Intercept Failed",Collections.<String,String>emptyMap(),is);}解决方法 您可以尝试实例化一个新的 WebResourceError,然后调用 WebViewClient.onReceivedError.从未尝试过这样的事情,但这就是它的样子: } catch (final IOException e) { WebResourceError wre = new WebResourceError(){ @OverrIDe public CharSequence getDescription (){ return e.toString(); } @OverrIDe public int getErrorCode (){ return WebVIEwClIEnt.ERROR_CONNECT; } }; onReceivedError(vIEw,request,wre); return true;} 如果您不想要或无法实例化WebResourceError,还可以使用deprecated version of onReceivedError.
以上是内存溢出为你收集整理的android – 如何处理WebViewClient.shouldInterceptRequest()中的IOException全部内容,希望文章能够帮你解决android – 如何处理WebViewClient.shouldInterceptRequest()中的IOException所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)