0

我在过去 2 天里遇到了这个 HTTPS 连接问题的问题,并且已经在网上搜索了解决方案。我遇到过 SSLSocketFactory 并尝试使用它,却发现我无法解决我的问题。我在加载 HTTPS 时没有收到任何 SSL 错误,而是在没有负载的情况下出现白屏。

我想知道的是,我开始了一个新的Android项目。只将 a 添加WebViewmain.xml中,然后我做一个loadURL(https website). 它返回一个没有 SSL 错误的白屏。在查看我尝试访问的 HTTPS 网站上显示的内容之前,我需要执行哪些步骤?是否需要使用 3rd 方 API?我可以下载 JAR 文件吗?

编辑:我没有收到任何 SSL 错误。我只看到这个:request time failed: java.net.SocketException: Address family not supported by protocol。知道我该如何处理吗?

4

1 回答 1

1

覆盖 WebViewClient 实现的方法,

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed(); // Ignore SSL certificate errors
}

试试下面的代码,https为我工作,

package org.example.webviewdemo;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;

public class WebViewDemo extends Activity {
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    private WebView webView;
    private EditText urlField;

    private Button goButton;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create reference to UI elements
        webView  = (WebView) findViewById(R.id.webview_compontent);
        urlField = (EditText)findViewById(R.id.url);
        goButton = (Button)findViewById(R.id.go_button);

        // workaround so that the default browser doesn't take over
        webView.setWebViewClient(new MyWebViewClient());

        // Setup click listener
        goButton.setOnClickListener( new OnClickListener() {
            public void onClick(View view) {
                openURL();
            }
        });

        // Setup key listener
        urlField.setOnKeyListener( new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if(keyCode==KeyEvent.KEYCODE_ENTER) {
                    openURL();
                    return true;
                } else {
                    return false;
                }
            }
        });

    }

    /** Opens the URL in a browser */
    private void openURL() {
        webView.loadUrl(urlField.getText().toString());
        webView.requestFocus();
    }    
}

主要的.xml

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">

    <EditText  
    android:id="@+id/url" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"     
    android:lines="1"
    android:layout_weight="1.0" android:hint="http://"/>

    <Button
    android:id="@+id/go_button"
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"     
    android:text="@string/go_button"
    />

</LinearLayout>

<WebView  
    android:id="@+id/webview_compontent"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1.0"   
/>

于 2012-02-28T05:49:32.727 回答