处理加载网络客户端 android

Handle loading webclient android

我的网络视图有问题。 当我打开网络成功时,我不知道如何处理网络客户端中的加载数据(超时)我关闭了 wifi 并继续打开网络客户端。它永远加载。(

这里是xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/web_assignment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <include
        android:id="@+id/retry_assignment"
        layout="@layout/layout_retry_web"
        android:visibility="gone"
        />
    <com.lusfold.spinnerloading.SpinnerLoading
        android:id="@+id/spinnerLoalding_assignment"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:visibility="gone"
        />
    <WebView
        android:id="@+id/layout_assignment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</RelativeLayout>

代码为:

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
   //
    tokenManager = new TokenManager(getApplicationContext());
    token = tokenManager.GetTokenID();

    if (networkStateReceiver.isConnected(this))
        checkTimeToken(token);
    else {
        setVisibleLayout();
    }
}
@Override
protected void onStart() {
    super.onStart();

}
@Override
protected void onResume() {
    super.onResume();

}

@Override
protected void onPause() {
    super.onPause();

}

@Override
protected void onStop() {
    super.onStop();
}

private void setVisibleLayout() {
    webView.setVisibility(View.GONE);
    relativeLayout.setVisibility(View.VISIBLE);
    spinnerLoading.setVisibility(View.GONE);
    btnRetry.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("onclick", "Tap reload");
            loadweb();
        }
    });
}
private void checkTimeToken(final String token) {

  //
}

private void loadweb() {
    final String strUrl = URL_Utils.Assignments_Load + token;
    Log.d("URL_asiagnmentss :", strUrl);

    if (networkStateReceiver.isConnectedNetwork(this)) {
        webView.loadUrl(strUrl);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                spinnerLoading.setVisibility(View.VISIBLE);
                webView.setVisibility(View.INVISIBLE);
                spinnerLoading.setPaintMode(1);
                spinnerLoading.setCircleRadius(20);
                spinnerLoading.setItemCount(8);
                Log.d("onPageStarted", "onPageStarted");
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

                spinnerLoading.setVisibility(View.INVISIBLE);
                webView.setVisibility(View.VISIBLE);
            }
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {

                super.onReceivedError(view, request, error);

            }

        });
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setSupportZoom(true);
        webView.setVisibility(View.VISIBLE);
        relativeLayout.setVisibility(View.GONE);
    } else
        setVisibleLayout();
} 

首先创建一个布尔变量默认为真,用于跟踪是否超时。

之后,在您的 WebViewClient 的 onPageStarted 方法上,您必须放置一个线程,该线程在给定时间触发。

最后,onPageFinished 设置变量 false

如代码所示:-

public class MyWebViewClient extends WebViewClient {
     boolean timeout = true;
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            Runnable run = new Runnable() {
                public void run() {

                    if(timeout) {
                        // do what you want
                        showAlert("Connection Timed out", "Whoops! Something went wrong. Please try again later.");
                    }
                }
            };
            myHandler.postDelayed(run, 5000);
        }

        public void onPageFinished(WebView view, String url) {
              timeout = false;
        }
}