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方式:

void android.webkit.WebView.loadData(String data, String mimeType, String encoding)

public void loadData (String data, String mimeType, String encoding)

Added in API level 1

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)

Added in API level 1

Loads the given URL.

Parameters
url the URL of the resource to load 

最新文章

  1. Microsoft Client Development MVP 2013 - 2014
  2. Java项目多数据源配置
  3. grep,sed,cut,awk,join个性特点
  4. Chapter 5
  5. 编写类String的构造函数、析构函数和赋值函数
  6. 【BZOJ】【2819】NIM
  7. C#.NET 各种连接字符串
  8. Nginx学习之二-配置项解析及编程实现
  9. Swift语言指南(六)--可选值
  10. 201521123068 《java程序设计》 第10周学习总结
  11. [.Net Core] 简单使用 Mvc 内置的 Ioc
  12. wince可用的7-zip
  13. combineByKey
  14. Visio画UML类图、序列图 for Java
  15. 使用Eclipse的坑
  16. nodejs+mysql入门实例(增)
  17. python之numpy.power()数组元素求n次方
  18. Java——word分词&#183;自定义词库
  19. (转)ASP.NET(C#)FileUpload实现上传限定类型和大小的文件到服务器
  20. Mac 隐私与安全没有允许任何来源选项

热门文章

  1. VC2010 MFC文档类菜单快捷键无法加载问题
  2. 通过keepalived实现 MySQL VIP 自动切换
  3. ThinkPHP中initialize和construct调用父类的区别
  4. 点击图标 标记为星标记事mac中修改默认的apache网站根目录位置
  5. C#加密NodeJS解密
  6. Android PopupWindow 点击消失解决办法
  7. OpenCV+QT开发环境(一):Windows环境
  8. 推荐acm题目
  9. ContextLoaderListener作用详解(转)
  10. sv_target_output dx11