问题
Hi everyone I am having an issue with an android webview : The webview works fine until I try to redirect to specific url ( which coincidentally happen to ask for cookies) "net::ERR_CONNECTION_RESET" is the error description and "-6" is the error code. Can anyone assist with how to fix this ? If I use the device browser it is working fine but with webview I get an error:
**My webview client**
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.util.Log;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.CookieManager;
import android.widget.Toast;
import android.content.Intent;
public class MyWebViewClient extends WebViewClient{
Context context;
TransactionHandler handler;
public MyWebViewClient (Context c, TransactionHandler handler){
this.context = c;
this.handler=handler;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("WebView", "Url is " + url);
if (url.contains(ApiConstants.payFailUrl)) {
handler.onFailed();
return false;
}
if (Uri.parse(url).getHost().contains(ApiConstants.payUrl)) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
if (Uri.parse(url).getHost().contains("the url i want to go to(should be here) which is triggering an error(i think it has something to do with them using cookies")) {
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// context.startActivity(intent);
//view.loadUrl(url);
// view.loadUrl("https://www.google.com.gh/");
// return true;
return super.shouldOverrideUrlLoading(view, url);
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
// Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
// context.startActivity(intent);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
Log.i("WebView", "pageFinished Url is " + url);
if (url.contains(ApiConstants.paySuccessUrl)) {
handler.onSuccess();
}
if (url.contains(ApiConstants.payUrlbeta)&&!url.contains("data:")) {
handler.onSuccess();
}
if (!DetectConnection.checkInternetConnection(context)&&!url.contains("blank")) {
Toast.makeText(context, "wrong " + url, Toast.LENGTH_LONG).show();
Utils.showAlert(context, "Please check your internet connection", "No Internet");
view.loadUrl("about:blank");
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
if (view.canGoBack()) {
view.goBack();
}
else{
view.loadUrl("about:blank");
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(context, "" + error.getErrorCode(), Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, "Error occurred", Toast.LENGTH_LONG).show();
}
}
}
来源:https://stackoverflow.com/questions/51518347/android-webview-error-code-6