CacheWebView icon indicating copy to clipboard operation
CacheWebView copied to clipboard

加载未完成时断网,之后一直报错[单例模式]

Open Loyea opened this issue 5 years ago • 9 comments

使用的是getInstance()的模式

加载未完成时断网,此后无论加载什么都是先走完超时后由webview原生重新进行加载(似乎是),而且缓存功能也失效了

报的都是这同一个错误

2019-04-28 12:54:47.873 17575-17733/com.xxx.xxx.debug W/System.err: java.net.SocketTimeoutException: timeout
2019-04-28 12:54:47.875 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http2.Http2Stream$StreamTimeout.newTimeoutException(Http2Stream.java:593)
2019-04-28 12:54:47.875 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http2.Http2Stream$StreamTimeout.exitAndThrowIfTimedOut(Http2Stream.java:601)
2019-04-28 12:54:47.875 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http2.Http2Stream.takeResponseHeaders(Http2Stream.java:146)
2019-04-28 12:54:47.875 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http2.Http2Codec.readResponseHeaders(Http2Codec.java:120)
2019-04-28 12:54:47.875 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:67)
2019-04-28 12:54:47.875 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
2019-04-28 12:54:47.876 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
2019-04-28 12:54:47.876 17575-17733/com.xxx.xxx.debug W/System.err:     at ren.yale.android.cachewebviewlib.HttpCacheInterceptor.intercept(HttpCacheInterceptor.java:20)
2019-04-28 12:54:47.876 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
2019-04-28 12:54:47.876 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
2019-04-28 12:54:47.876 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
2019-04-28 12:54:47.876 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
2019-04-28 12:54:47.877 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
2019-04-28 12:54:47.877 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
2019-04-28 12:54:47.877 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
2019-04-28 12:54:47.877 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
2019-04-28 12:54:47.877 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at okhttp3.RealCall.execute(RealCall.java:63)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at ren.yale.android.cachewebviewlib.WebViewCacheInterceptor.interceptRequest(WebViewCacheInterceptor.java:297)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at ren.yale.android.cachewebviewlib.WebViewCacheInterceptor.interceptRequest(WebViewCacheInterceptor.java:127)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at ren.yale.android.cachewebviewlib.WebViewCacheInterceptorInst.interceptRequest(WebViewCacheInterceptorInst.java:44)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at com.xxx.xxx.activity.NativeWebviewImageBrowserActivity$1.shouldInterceptRequest(NativeWebviewImageBrowserActivity.java:274)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at com.android.webview.chromium.WebViewContentsClientAdapter.shouldInterceptRequest(WebViewContentsClientAdapter.java:54)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at org.chromium.android_webview.AwContents$BackgroundThreadClientImpl.shouldInterceptRequest(AwContents.java:9)
2019-04-28 12:54:47.878 17575-17733/com.xxx.xxx.debug W/System.err:     at org.chromium.android_webview.AwContentsBackgroundThreadClient.shouldInterceptRequestFromNative(AwContentsBackgroundThreadClient.java:11)

Loyea avatar Apr 28 '19 05:04 Loyea

加载未完成应该就没有缓存,超时后交给系统webview处理了,系统webview应该判断断网,处理速度也应该很快的

yale8848 avatar Apr 28 '19 08:04 yale8848

问题是以后无论请求啥,网络状态也没问题,都会报这个错,这个库的功能就彻底失效了

Loyea avatar Apr 28 '19 08:04 Loyea

你意思,先联网,再断网,再联网,一直都会报这个错吗?断网后再联网应该能正常访问的,这个我都是测试过的。

yale8848 avatar Apr 28 '19 08:04 yale8848

@yale8848 对的。写了个简单的demo 我截了个视频: https://www.youtube.com/watch?v=bwWOz_Yn75A

这是代码:

package com.loyea.myapplication;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import ren.yale.android.cachewebviewlib.WebViewCacheInterceptor;
import ren.yale.android.cachewebviewlib.WebViewCacheInterceptorInst;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    WebView webview;

    String[] urls = {
            "https://nmbimg.fastmirror.org/image/2019-04-28/5cc4f02e98ee1.png",
            "https://nmbimg.fastmirror.org/image/2019-04-28/5cc553c110b79.jpg",
            "https://nmbimg.fastmirror.org/image/2019-03-20/5c91a9bd7c8d7.jpg",
            "https://nmbimg.fastmirror.org/image/2019-04-28/5cc5625c6483e.jpg",
            "https://nmbimg.fastmirror.org/image/2019-04-28/5cc54bed8c66b.jpg",
            "https://nmbimg.fastmirror.org/image/2019-04-28/5cc5326fb3320.jpg"};

    int index = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webview = findViewById(R.id.webview);

        WebViewCacheInterceptorInst.getInstance().
                init(new WebViewCacheInterceptor.Builder(getApplicationContext()));


        webview.setWebViewClient(new WebViewClient() {

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView webView, String s) {
                webView.loadUrl(s);
                return true;
            }

            @Override
            public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
                super.onPageStarted(webView, s, bitmap);
            }

            @Override
            public void onPageFinished(WebView webView, String s) {
                super.onPageFinished(webView, s);
                Log.d(TAG, "onPageFinished() called with: webView = [" + webView + "], s = [" + s + "]");
            }

            @Override
            public void onReceivedError(WebView webView, int i, String s, String s1) {
                super.onReceivedError(webView, i, s, s1);
                Log.d(TAG, "onReceivedError() called with: webView = [" + webView + "], i = [" + i + "], s = [" + s + "], s1 = [" + s1 + "]");
            }

            @Override
            public void onLoadResource(WebView webView, String s) {
                super.onLoadResource(webView, s);
            }

            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                Log.d(TAG, "shouldInterceptRequest() called with: view = [" + view + "], request = [" + request.getUrl() + "]");
                return  WebViewCacheInterceptorInst.getInstance().interceptRequest(request);
            }

            @Nullable
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                Log.d(TAG, "shouldInterceptRequest() called with: view = [" + view + "], url = [" + url + "]");
                Log.d(TAG, "shouldInterceptRequest: " + url);
                return  WebViewCacheInterceptorInst.getInstance().interceptRequest(url);
            }
        });
    }

    public void load(View view) {
        webview.loadUrl(urls[index++]);

    }
}

Loyea avatar Apr 28 '19 09:04 Loyea

我测试几个图片ok,youtub +vpn 按照你说的模式也ok;那麻烦你提供更多代码,以便分析问题

yale8848 avatar Apr 28 '19 10:04 yale8848

就上面这点代码就能复现啊。。。。而且还是100%复现的 我视频里面就是这个 没别的代码了 布局就一个button和webview 我的视频不够详细么

Loyea avatar Apr 28 '19 11:04 Loyea

另外没有使用代理

Loyea avatar Apr 28 '19 11:04 Loyea

用非单例模式先规避了,还是很好用的。这个bug我也看不懂原因,麻烦你了

Loyea avatar Apr 28 '19 11:04 Loyea

你用真机再试试,看还有没有问题

yale8848 avatar Apr 29 '19 01:04 yale8848