(一)WebView的用法
1、WebView也是一个普通的控件。
2、常用用法:
1 WebView webView = (WebView)findViewById(R.id.web_view);
2 webView.getSettings( ).setJavaScriptEnabled(true); //让webView支持javascript脚本
3 webView.setWebViewClient(new WebViewClient( ){
4 @Override
5 public boolean shouldOverrideUrlLoading(WebView view, String url){
6 view.loadUrl(url); //根据传入的参数再去加载新的网页
7 return true; //表示当前WebView可以处理打开新网页的请求,不用借助系统浏览器
8 }
9 });
10 webView.loadUrl("http://www.baidu.com");
3、使用任何网络功能的程序都要申请权限:
1 <uses-permission android:name="android.permission.INTERNET" />
(二)使用HttpURLConnection访问网络
1、步骤如下:
(1)创建一个URL对象,然后使用其openConnection创建一个HttpURLConnection对象:
1 URL url = new URL("http://www.baidu.com");
2 connection = (HttpURLConnection) url.openConnection();
(2)设置HttpURLConnection是GET方法还是POST方法:
1 connection.setRequestMethod("GET");
(3)对HttpURLConnection进行其他的设置:
1 connection.setConnectTimeout(8000); //设置连接超时的毫秒数
2 connection.setReadTimeout(8000); //设置读取超时的毫秒数
(4)用HttpURLConnection对象的getInputStream方法获取服务器的返回输入流InputStream对象:
1 InputStream in = connection.getInputStream();
(5)对输入流进行读取:
1 BufferedReader reader = new BufferedReader(
2 new InputStreamReader(in));
3 StringBuilder response = new StringBuilder();
4 String line;
5 while ((line = reader.readLine()) != null) {
6 response.append(line);
7 }
(6)用disconnect方法关闭这个HTTP连接:
1 connection.disconnect();
2、注意:
(1)网络请求要放在子线程里。
(2)在子线程里网络请求获取返回数据后,如果要进行UI操作,则要采用异步消息处理方法。
(3)要申请网络权限。
(三)使用HttpClient访问网络
1、HttpClient是一个接口类,是Apache提供的HTTP网络访问接口,从一开始就被引入到了Android API中。
2、使用步骤:
(1)创建HttpClient实例:
1 HttpClient httpClient = new DefaultHttpClient();
(2)根据发起请求的类型不同,下面的第(2)步的步骤也不同:
①GET请求:
1 HttpGet httpGet = new HttpGet("http://10.0.2.2:8081/get_data.xml");
②POST请求:
1 HttpPost httpPost = new HttpPost("http://www.baidu.com");
2 List<NameValuePair> params = new ArrayList<NameValuePair>();
3 params.add(new BasicNameValuePair("username","admin"));
4 params.add(new BasicNameValuePair("password","123456"));
5 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8");
6 httpPost.setEntity(entity);
(3)获取服务器返回值:
1 HttpResponse httpResponse = httpClient.execute(httpGet);
(4)判断返回状态码,如果等于200就表示请求和响应都成功了:
1 if (httpResponse.getStatusLine().getStatusCode() == 200) {
2 HttpEntity entity = httpResponse.getEntity();
3 String response = EntityUtils.toString(entity, "utf-8");
4 ... //其他操作
5 }
3、注意:HttpClient访问网络同样要放在子线程里、申请网络权限。
(四)使用回调机制封装HttpURLConnection操作来创建HttpUtil类
1、创建HttpCallbackListener接口:
1 public interface HttpCallbackListener {
2 void onFinish(String response); //在服务器成功响应请求时调用
3 void onError(Exception e); //进行网络操作出错时调用
4 }
2、创建HttpUtil类:
1 import java.io.BufferedReader;
2 import java.io.InputStream;
3 import java.io.InputStreamReader;
4 import java.net.HttpURLConnection;
5 import java.net.URL;
6
7 public class HttpUtil {
8 public static void sendHttpRequest(final String address,final HttpCallbackListener listener) {
9 new Thread(new Runnable() {
10 @Override
11 public void run() {
12 HttpURLConnection connection = null;
13
14 try {
15 URL url = new URL(address);
16 connection = (HttpURLConnection) url.openConnection();
17
18 connection.setRequestMethod("GET");
19 connection.setConnectTimeout(8000);
20 connection.setReadTimeout(8000);
21 connection.setDoInput(true);
22 connection.setDoOutput(true);
23
24 InputStream in = connection.getInputStream();
25 BufferedReader reader = new BufferedReader(
26 new InputStreamReader(in));
27 StringBuilder response = new StringBuilder();
28 String line;
29
30 while ((line = reader.readLine()) != null) {
31 response.append(line);
32 }
33
34 if (listener != null) {
35 // 回调onFinish方法
36 listener.onFinish(response.toString());
37 }
38
39 } catch (Exception e) {
40 if (listener != null) {
41 listener.onError(e);
42 }
43 } finally {
44 if (connection != null) {
45 connection.disconnect();
46 }
47 }
48 }
49 }).start();
50 }
51 }
3、使用时这样使用:
1 HttpUtil.sendHttpRequest("http://www.baidu.com",new HttpCallBackListener(){
2 @Override
3 public void onFinish(String response){
4 //在这里根据返回内容执行具体的逻辑
5 }
6
7 @Override
8 public void onError(Exception e){
9 //在这里对异常情况进行处理
10 }
11 });
(五)解析XML数据
1、安装Apache服务器:下载安装包,然后安装成功后启动服务器,在浏览器里输入127.0.0.1,会看到Apache的字样。
2、在Apache安装目录:...Apachehtdocs目录下,可以新建一个xml文件,命名为get_data.xml,加入内容,然后在浏览器里输入:127.0.0.1/get_data.xml(在手机模拟器里要输入:10.0.2.2/get_data.xml),就会显示出该文件的内容。
3、编写的XML数据的格式如下:
1 <apps>
2 <app>
3 <id>1</id>
4 <name>Google Maps</name>
5 <version>1.0</version>
6 </app>
7 <app>
8 <id>2</id>
9 <name>Chrome</name>
10 <version>1.8</version>
11 </app>
12 <app>
13 <id>3</id>
14 <name>Google Play</name>
15 <version>3.2</version>
16 </app>
17 </apps>
4、用Pull方式解析XML数据:
1 private void parseXMLWithPull(String xmlData) {
2 try {
3 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
4 XmlPullParser xmlPullParse = factory.newPullParser();
5 xmlPullParse.setInput(new StringReader(xmlData));
6 int eventType = xmlPullParse.getEventType();
7
8 String id = "";
9 String name = "";
10 String version = "";
11
12 while (eventType != XmlPullParser.END_DOCUMENT) {
13 String nodeName = xmlPullParse.getName();
14 switch (eventType) {
15 // 开始解析某个结点
16 case XmlPullParser.START_TAG: {
17 if ("id".equals(nodeName)) {
18 id = xmlPullParse.nextText();
19 } else if ("name".equals(nodeName)) {
20 name = xmlPullParse.nextText();
21 } else if ("version".equals(nodeName)) {
22 version = xmlPullParse.nextText();
23 }
24 }
25 break;
26 // 完成解析某个结点
27 case XmlPullParser.END_TAG: {
28 if ("app".equals(nodeName)) {
29 Log.d("MainActivity", "id is " + id);
30 Log.d("MainActivity", "name is " + name);
31 Log.d("MainActivity", "version is " + version);
32 }
33 }
34 break;
35 default:
36 break;
37 }
38
39 eventType = xmlPullParse.next();
40 }
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 }
5、用SAX方式解析XML数据步骤:
(1)创建ContentHandler 类继承自DefaultHandler并重写5个方法:
1 import org.xml.sax.Attributes;
2 import org.xml.sax.SAXException;
3 import org.xml.sax.helpers.DefaultHandler;
4
5 import android.util.Log;
6
7 public class ContentHandler extends DefaultHandler {
8 private String nodeName;
9 private StringBuilder id;
10 private StringBuilder name;
11 private StringBuilder version;
12
13 @Override
14 public void startDocument() throws SAXException {
15 id = new StringBuilder();
16 name = new StringBuilder();
17 version = new StringBuilder();
18 }
19
20 @Override
21 public void startElement(String uri, String localName, String qName,
22 Attributes attributes) throws SAXException {
23 // 记录当前结点名
24 nodeName = localName;
25 }
26
27 @Override
28 public void characters(char[] ch, int start, int length)
29 throws SAXException {
30 // 根据当前结点名判断将内容添加到哪一个StringBuilder对象中
31 if ("id".equals(nodeName)) {
32 id.append(ch, start, length);
33 } else if ("name".equals(nodeName)) {
34 name.append(ch, start, length);
35 } else if ("version".equals(nodeName)) {
36 version.append(ch, start, length);
37 }
38 }
39
40 @Override
41 public void endElement(String uri, String localName, String qName)
42 throws SAXException {
43 // 用trim方法去掉空白字符
44 if ("app".equals(localName)) {
45 Log.d("MainActivity", "id is " + id.toString().trim());
46 Log.d("MainActivity", "name is " + name.toString().trim());
47 Log.d("MainActivity", "version is " + version.toString().trim());
48
49 // 将StringBuilder清空
50 id.setLength(0);
51 name.setLength(0);
52 version.setLength(0);
53 }
54 }
55
56 @Override
57 public void endDocument() throws SAXException {
58 }
59 }
(2)写具体方法:
1 private void parseXMLWithSAX(String xmlData) {
2 try {
3 SAXParserFactory factory = SAXParserFactory.newInstance();
4 XMLReader xmlReader = factory.newSAXParser().getXMLReader();
5 ContentHandler handler = new ContentHandler();
6
7 xmlReader.setContentHandler(handler);
8
9 // 开始执行解析
10 xmlReader.parse(new InputSource(new StringReader(xmlData)));
11 } catch (Exception e) {
12 e.printStackTrace();
13 }
14 }
(六)解析Json数据
见我的博客园文章:用GSON解析Json格式数据,这里不再详写。
【本章结束】