Android中WebView與銀聯對接空白頁問題

  • 測試佈局代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView 
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:id="@+id/webView"
    />

</RelativeLayout>
  • 在AndroidManifest.xml中增加
<uses-permission android:name="android.permission.INTERNET"/>
  • 代碼
package com.example.testwebview;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;

public class MainActivity extends Activity {

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

		String url = "http://192.168.2.68/UnPayTest/Form_6_2_FrontConsume.aspx";

		WebView webView = (WebView) findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true);
		// WebView加載web資源
		webView.loadUrl(url);
		// 覆蓋WebView默認使用第三方或系統默認瀏覽器打開網頁的行爲,使網頁用WebView打開
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			@Override
		    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
		        handler.proceed();
		    }
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
  • 核心在哪呢?
		webView.getSettings().setJavaScriptEnabled(true);// 這裏,允許執行js腳本
		webView.setWebViewClient(new WebViewClient(){
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
			@Override
		    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
		        handler.proceed();// 這裏,繼續執行ssl
		    }
		});

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章