学习webView控件使用
WebView 对象用于网页显示使用,简单的学习并使用了一下。
1、首先在 layout 中摆一个全屏的 webview 控件 (main.xml )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
2、抽象出一个简单的 TestWebView 类,很简单,直接上代码:
package com.example.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
public class TestWebView {
private static final String TAG = "TestWebView";
private reponseEvent re = null;
public TestWebView() {
}
public static interface reponseEvent {
public void onResult(String err,String result);
}
public void setListener(reponseEvent re) {
this.re = re;
}
public void requestUrl(final String _url) {
Thread th = new Thread(new Runnable() {
@Override
public void run() {
String line = null;
BufferedReader buffer = null;
StringBuffer sb = new StringBuffer();
try {
URL url = new URL(_url);
HttpURLConnection urlConn = (HttpURLConnection) url
.openConnection();
buffer = new BufferedReader(new InputStreamReader(urlConn
.getInputStream(), "GB2312"));
while ((line = buffer.readLine()) != null) {
sb.append(line);
}
} catch (MalformedURLException e) {
Log.e(TAG, "MalformedURLException: " + e.getMessage());
} catch (IOException e) {
Log.e(TAG, "IOException: " + e.getMessage());
}
if (re != null)
re.onResult(null, sb.toString());
}
});
th.start();
}
}
3、用法
package com.example.test;
import java.io.UnsupportedEncodingException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import com.example.test.TestWebView.reponseEvent;
public class MainActivity extends Activity {
static final String TAG = "MainActivity";
private WebView webView;
private WebHandler webHandler = new WebHandler();
private TestWebView testWebview = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
// testView();
testView1();
}
void initview() {
webView = (WebView) findViewById(R.id.webView1);
}
void testView() {
testWebview = new TestWebView();
testWebview.setListener(new reponseEvent() {
@Override
public void onResult(String err, String result) {
Message msg = webHandler.obtainMessage();
msg.what = 1;
msg.obj = (String) result;
webHandler.sendMessage(msg);
}
});
testWebview.requestUrl("http://www.sina.com.cn/index.shtml");
}
void testView1() {
webView.loadUrl("http://www.sina.com.cn/index.shtml");
}
class WebHandler extends Handler {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
String s = (String) msg.obj;
try {
s = new String(s.getBytes("GB2312"), "GB2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
webView.getSettings().setDefaultTextEncodingName("UTF-8");
webView.loadData(s, "text/html; charset=UTF-8", null);
break;
default:
break;
}
};
};
}
使用了两种方式来调用加载url方式:
public void loadData (String data, String mimeType, String encoding)
Loads the given data into this WebView using a 'data' scheme URL.
Note that JavaScript's same origin policy means that script running in a page loaded using this method will be unable to access content loaded using any scheme other than 'data', including 'http(s)'. To avoid this restriction, use loadDataWithBaseURL()
with an appropriate base URL.
The encoding parameter specifies whether the data is base64 or URL encoded. If the data is base64 encoded, the value of the encoding parameter must be 'base64'. For all other values of the parameter, including null, it is assumed that the data uses ASCII encoding for octets inside the range of safe URL characters and use the standard %xx hex encoding of URLs for octets outside that range. For example, '#', '%', '\', '?' should be replaced by %23, %25, %27, %3f respectively.
The 'data' scheme URL formed by this method uses the default US-ASCII charset. If you need need to set a different charset, you should form a 'data' scheme URL which explicitly specifies a charset parameter in the mediatype portion of the URL and call loadUrl(String)
instead. Note that the charset obtained from the mediatype portion of a data URL always overrides that specified in the HTML or XML document itself.
public void loadUrl (String url)
Loads the given URL.
Parameters
url | the URL of the resource to load |
---|