学习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编程类似客户端和服 ...
随机推荐
- DRP PK 牛腩新闻发布系统
一.JSP与ASP (1)Web服务器的支持:大多数通用的Web服务器如:Apache.Netscape和Microsoft IIS都支持JSP页面,只有微软本身的Microsoft IIS和Pers ...
- 9、XAML名称空间详解
XAML命名空间 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...
- cocos2dx中的背景图层CCLayerColor和渐变图层CCLayerGradient
1.CCLayerColor是专门用来处理背景颜色的图层,它继承自CCLayer,可以用来设置图层的背景颜色,因为CCLayer默认是透明色的,即无颜色的 2.CCLayerGradient是用来显示 ...
- 团队项目之NABC
Time:2013-10-22 Author:wang 一个成功的人,总是知道如何管理自己的时间,如何让自己的时间得到最充分最有效的利用.对学生一族而言,课业负担重,各种课程.各种活动.各种社团,如果 ...
- OpenWrt固件刷入后串口终端没有反应的问题
[路由器开发板硬件固件配置] MTK双频:MT7620a + MT7612e 内存:256 MB 闪存:16 MB 固件:MTK自带SDK中的OpenWrt固件(mtksdk-openwrt-2.6. ...
- 悲惨的Android程序员
Android程序员太悲惨了,连Android官网都访问不了,整个Android程序员的水平都被拉低了一个等级.受不了了.说说悲惨的遭遇吧. 起源:高射炮打苍蝇,驴受伤了 Android一个纯技术网站 ...
- 1996: [Hnoi2010]chorus 合唱队 - BZOJ
Description Input Output Sample Input41701 1702 1703 1704Sample Output8HINT 水题,区间dp,f[l,r,k]表示区间[l,r ...
- codeforces GYM 100114 J. Computer Network 无相图缩点+树的直径
题目链接: http://codeforces.com/gym/100114 Description The computer network of “Plunder & Flee Inc.” ...
- oracle——merge
一.概述 使用merge声明从一个或者更多个表或视图中筛选记录,以用来更新或者插入到一个表或视图中.你可以指定条件以决定是执行update操作还是insert操作到目标表或视图中. 这个声明是一个 ...
- setDepthStencilState
cgfx->hlsl StencilFunc={always,1,0xff}->setDepthStencilState(DepthState,0) StencilFunc={always ...
void android.webkit.