学习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 |
|---|
学习webView控件使用的更多相关文章
- Android学习笔记50:使用WebView控件浏览网页
在Android中,可以使用Webview控件来浏览网页.通过使用该控件,我们可以自制一个简单的浏览器,运行效果如图1所示. 图1 运行效果 1.WebView 在使用WebView控件时,首先需要在 ...
- 安卓开发学习笔记(五):史上最简单且华丽地实现Android Stutio当中Webview控件https/http协议的方法
一.我们先在XML当中自定义一个webview(Second_layout.xml) 代码如下: <?xml version="1.0" encoding="utf ...
- 【WP开发】WebView控件应用要点
WebView控件我就不多作详细的介绍,相信大家都懂的,就算你没用过,你看他的名字和长相都知道它的用途了.就是用来显示HTML内容的. 在WP 8.1的Runtime App中,这个控件大致有以下几个 ...
- 通过webview控件访问网页
初学android开发,在界面编辑器上放了一个webview控件,可惜不知道如何访问控件,在网上看到一段代码记录下来,算是第一次学习笔记. 要想让程序联网需要授权,在AndroidManifest.x ...
- (转载)android控件之WebView控件缩小
android控件之WebView控件缩小 作者: 字体:[增加 减小] 类型:转载 时间:2013-05-16我要评论 发现这个控件挺好用,能自已控制进度条,而且这个控件的功能非常壮大,先上个简单的 ...
- Delphi 7学习开发控件
我们知道使用Delphi快速开发,很大的一方面就是其强大的VCL控件,另外丰富的第三方控件也使得Delphi程序员更加快速的开发出所需要的程序.在此不特别介绍一些概念,只记录自己学习开发控件的步骤.假 ...
- android WebView控件显示网页
有时需要app里面显示网页,而不调用其他浏览器浏览网页,那么这时就需要WebView控件.这个控件也是很强大的,放大,缩小,前进,后退网页都可以. 1.部分方法 //支持javascriptweb.g ...
- Android中通过WebView控件实现与JavaScript方法相互调用的地图应用
在Android中通过WebView控件,可以实现要加载的页面与Android方法相互调用,我们要实现WebView中的addJavascriptInterface方法,这样html才能调用andro ...
- 过实现鹰眼图这个功能来进一步学习MapControl控件
我们通过实现鹰眼图这个功能来进一步学习MapControl控件.在实现鹰眼图之前,我们需 要接口有更深入的了解. 变主动为被动-出接口(OutBound interface) COM编程类似客户端和服 ...
随机推荐
- Python默认模块 os和shutil 实用函数
os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是 ' ...
- php中调用用户自定义函数的方法:call_user_func,call_user_func_array
看UCenter的时候有一个函数call_user_func,百思不得其解,因为我以为是自己定义的函数,结果到处都找不到,后来百度了一下才知道call_user_func是内置函数,该函数允许用户调用 ...
- 图片轮播插件-carouFredSel
carouFredSel图片轮播插件基于Jquery,比较常规的轮播插件,支持滚轮及键盘左右按键,加入其它插件可实现更加复杂的特效. 主页地址:http://caroufredsel.dev7stud ...
- 【转】edittext设置点击链接
I put some ClickableSpan in EditText, unfortunately, that spans are still not clickable. When I clic ...
- 【转,未试】android 9path教程与去黑边
本帖最后由 ArcherFMY 于 2013-4-24 17:39 编辑 <ignore_js_op> draw9patch.zip (124.83 KB) 这是Draw9patch& ...
- Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
[Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used ...
- C++排序函数sort/qsort使用
问题描述: C++排序函数sort/qsort的使用 问题解决: (1)sort函数使用 注: sort函数,参数1为数组首地址,参数2是数组 ...
- 剑指offer--面试题9
题目一:求斐波那契数列第n项 自己所写代码如下: #include "stdafx.h" #include<iostream> long Fibonacci(unsig ...
- Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi
Taxi Trip Time Winners' Interview: 3rd place, BlueTaxi This spring, Kaggle hosted two competitions w ...
- ajax post 跨域
H5页面永远无法避开跨域问题-- php中, header('Access-Control-Allow-Origin:*'); 搞定. 兼容性先不管了. 来自为知笔记(Wiz)
void android.webkit.