访问android webview

时间:2016-07-19 03:08:57

标签: android android-webview

我正在创建一个应用程序,需要能够访问某些网站,以便将javascript注入这些网站。但是,当我尝试时,我需要访问的网站不会加载。这是我活动的代码

import android.content.SharedPreferences;
import android.media.audiofx.BassBoost;
import android.preference.PreferenceManager;
import android.support.v4.content.SharedPreferencesCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class ConvergintActivity extends AppCompatActivity {

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_convergint);

        webView = (WebView) findViewById(R.id.convergintWebView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.clearSslPreferences();
        webView.loadUrl("https://infoshare.convergint.com/login.php");
        webView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                String username = sharedPrefs.getString("convergintUsername", "");
                Log.e("Debug!", "Username = "+ username);
            }

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

我要加载的网页是https://infoshare.convergint.com/login.php。为什么www.google.com会加载但infoshare.convergint.com/login.php不会?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您是否尝试在网页浏览中加载该页面?您应该设置 shouldOverrideUrlLoading()以返回 false

但是,您可以添加此设置,

webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

要在Webview中检查错误响应,请在WebViewClient方法中添加

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error){
                        //do something here
}