大家好,我在使用android webview时遇到问题:在我尝试将其重定向到特定的URL(碰巧要问cookie)之前,webview可以正常工作。“ net :: ERR_CONNECTION_RESET”是错误说明,而“ -6 “是错误代码。谁能协助解决此问题?如果我使用设备浏览器,则可以正常运行,但是使用webview时会出现错误:
**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();
}
}
}