表单提交后,WebView无法加载页面

时间:2017-04-26 22:13:46

标签: android webview react-native

为了解决我使用React Native与WebView link组件的问题,我在android中进行了直接测试,在尝试加载特定页面时获得了相同的错误。

第一页包含一个表单,但在输入用户数据并提交后,它没有显示下一页,只显示“正在加载...”的消息

这是我的android代码:

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview_main);

    webView = (WebView) findViewById(R.id.webView1);
    webView.clearCache(true);
    webView.setWebViewClient(new WebViewClient());
    webView .getSettings().setJavaScriptEnabled(true);
    webView .getSettings().setDomStorageEnabled(true);
    webView.loadUrl("https://auth.mercadopago.com.ve/authorization?client_id=6012820321480442&response_type=code&platform_id=mp&redirect_uri=https%3A%2F%2Fus-central1-quicpyapp.cloudfunctions.net%2FmercadoPagoConnect%3FuserId%3DcljvLoUJNoOHfe636ofMHduXu2o2");

}

}

如果我评论以下行,我也能够观察到页面正常工作:

//webView.setWebViewClient(new WebViewClient());

测试用户数据如下:“user”:“TETE5691798”,“password”:“qatest6094”

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:0)

通过添加onReceivedHttpAuthRequest(),它会自动对您的网站进行身份验证。请尝试以下代码:

      mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedHttpAuthRequest(WebView view, HttpAuthHandler handler, String host, String realm) {
                mWebView.setHttpAuthUsernamePassword(host, realm, YOUR_USER_NAME, YOUR_PASSWORD);
                handler.proceed(YOUR_USER_NAME, YOUR_PASSWORD);
            }

      }

答案 1 :(得分:0)

尝试以下解决方案:

var currentUrl = "google.com" 
var partOfUrl = currentUrl.substring(0, currentUrl.length-2)

webView.setWebViewClient(object: WebViewClient() {

 override fun onLoadResource(WebView view, String url) {
 //call loadUrl() method  here 
 // also check if url contains partOfUrl, if not load it differently.
 if(url.contains(partOfUrl, true)) {
     //it should work if you reach inside this if scope.
 } else if(!(currentUrl.startWith("w", true))) {
     webView.loadurl("www.$currentUrl")

 } else if(!(currentUrl.startWith("h", true))) {
     webView.loadurl("https://$currentUrl")

 } else { 
  //...
 }


 }

 override fun onReceivedSslError(view: WebView?, handler: SslErrorHandler?, error: SslError?) {
// you can call again loadUrl from here too if there is any error.
}
//You should also override other override method for error such as onReceiveError to see how all these methods are called one after another and how they behave while debugging with break point. 
}