AndroidManifest.xml文件中加入网络访问的权限设置:
<uses-permission android:name="android.permission.INTERNET" />
activity_main.xml代码
<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="com.jt.http_01.MainActivity" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
WebViewLoadData.java代码
/**
* 使用WebView加载 HTML 代码
* @author jiatao
* @date 2015-5-2
* @version 1.0
*/
package com.jt.http_01;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewLoadData extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initDisplay();
}
public void initDisplay() {
// TODO Auto-generated method stub
webView = (WebView) findViewById(R.id.webView1);
String htmlCode = "<html>"
+ "<head>"
+ "<title>Welcome</title>"
+ "</head>"
+ "<body>"
+ "<h2>Welcome to <a href=\"http://www.163.com\">欢迎来到网易</a></h2>"
+ "</body>" + "</html>";
/**
* 调用loadData()方法加载HTML代码
* public void loadData(String data, String mimeType, String encoding) { }
*/
webView.loadData(htmlCode, "text/html;utf-8", "utf-8");
}
}