WebResourceResponse导致ClassNotFoundException忽略@TargetApi子句

时间:2015-05-08 00:01:14

标签: java android caching service webview

我有一段简单的代码:

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ) return null;
        ...
                try {
                    File input = new File(fullPathToCachedFile);
                    if ( input.exists() == false ) return null;

                    final BufferedInputStream stream = new BufferedInputStream(new FileInputStream(input));
                    return new WebResourceResponse(mimeType, "UTF-8", stream);
                }
                catch (IOException e) { e.printStackTrace(); }
        ...

        return null;
    }

这条线让我很生气:

return new WebResourceResponse(mimeType, "UTF-8", stream);

在Android 4.4上(例如)整个项目运行良好,但我在Android 2.2和2.3上尝试过..然后控制台说不知道&#34; WebResourceResponse&#34;类。

Thread [<1> main] (Suspended (exception ClassNotFoundException))    
    <VM does not provide monitor information>   
    PathClassLoader.findClass(String) line: 243 
    PathClassLoader(ClassLoader).loadClass(String, boolean) line: 573   
    PathClassLoader(ClassLoader).loadClass(String) line: 532    
    WebViewExt.defaultConfig() line: 52 (here is about to use WebResourceResponse)
    WebViewExt.init(ForegroundService) line: 35 
    ForegroundService.startWebView(WebViewExt) line: 88 
    MyApplication$1.onServiceConnected(ComponentName, IBinder) line: 78 
    ActivityThread$PackageInfo$ServiceDispatcher.doConnected(ComponentName, IBinder) line: 1247 
    ActivityThread$PackageInfo$ServiceDispatcher$RunConnection.run() line: 1264 
    ActivityThread$H(Handler).handleCallback(Message) line: 587 
    ActivityThread$H(Handler).dispatchMessage(Message) line: 92 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4627    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 868  
    ZygoteInit.main(String[]) line: 626 
    NativeStart.main(String[]) line: not available [native method]  

为什么我知道,它是关于&#34; WebResourceResponse&#34;类?没有提到的线项目运行良好:

/*return new WebResourceResponse(mimeType, "UTF-8", stream);*/

没有它项目运行良好,没有错误和例外。

@TargetAPI不应阻止此&#34; WebResourceResponse&#34;的可见性。适用于Android的课程?它是如何工作的以及如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

我以某种方式解决了它。但这是一个奇怪的解决方案。 if语句

if ( Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB ) return null;

不在shouldInterceptRequest函数内部工作,但在设置WebViewClient自定义类之外工作:

if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB )
    setWebViewClient(new WebViewClientExt2(this)); //with shouldInterceptRequest 
else
    setWebViewClient(new WebViewClientExt(this)); //without