Chinaunix首页 | 论坛 | 博客
  • 博客访问: 869239
  • 博文数量: 322
  • 博客积分: 6688
  • 博客等级: 准将
  • 技术积分: 3626
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-19 11:26
文章分类

全部博文(322)

文章存档

2013年(5)

2012年(66)

2011年(87)

2010年(164)

分类: Java

2011-01-07 09:35:47

在上获取资源

org.wp.activity

GetXmlTest

Java代码

  1. package org.wp.activity;
  2.  
  3. import java.net.URLEncoder;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.File;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStreamWriter;
  9. import org.wp.domain.Resource;
  10. import org.wp.net.utils.NetTool;
  11. import org.wp.net.utils.SaxTool;
  12. import android.os.Environment;
  13. import android.test.AndroidTestCase;
  14. import android.util.Log;
  15.  
  16. public class GetXmlTest extends AndroidTestCase {
  17.  
  18.     private static final String TAG = "GetXmlTest";
  19.  
  20.     public void testGetXml() throws Exception {
  21.         // 如果参数是中文必须进行Url编码
  22.         // URLEncoder.encode("http://", "UTF-8");
  23.         String path = "";
  24.         String xml = NetTool.getTextContent(path, "UTF-8");
  25.         FileOutputStream outputStream = new FileOutputStream(new File(
  26.                 Environment.getExternalStorageDirectory(), "test.xml"));
  27.         OutputStreamWriter writer = new OutputStreamWriter(outputStream,
  28.                 "UTF-8");
  29.         writer.write(xml);
  30.         writer.flush();
  31.         writer.close();
  32.         outputStream.close();
  33.         Log.i(TAG, xml);
  34.     }
  35.  
  36.     public void testGetXmlAndParser1() throws Exception {
  37.         String path = "";
  38.         String xml = NetTool.getTextContent(path, "UTF-8");
  39.         InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
  40.         Resource resource = SaxTool.readXml(inputStream);
  41.         Log.i(TAG, resource.toString());
  42.     }
  43.  
  44.     public void testGetXmlAndParser2() throws Exception {
  45.         String path = "";
  46.         InputStream inputStream = NetTool.getContent(path, "UTF-8");
  47.         if (inputStream != null) {
  48.             Resource resource = SaxTool.readXml(inputStream);
  49.             Log.i(TAG, resource.toString());
  50.         } else {
  51.             Log.i(TAG, "get url content error");
  52.         }
  53.     }
  54. }

org.wp.net.utils

NetTool

Java代码

  1. package org.wp.net.utils;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.URL;
  7.  
  8. public class NetTool {
  9.  
  10.     public static InputStream getContent(String urlpath, String encoding)
  11.             throws Exception {
  12.         URL url = new URL(urlpath);
  13.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  14.         conn.setRequestMethod("GET");
  15.         conn.setConnectTimeout(6 * 1000);
  16.         if (conn.getResponseCode() == 200) {
  17.             return conn.getInputStream();
  18.         }
  19.         return null;
  20.     }
  21.  
  22.     public static String getTextContent(String urlpath, String encoding)
  23.             throws Exception {
  24.         URL url = new URL(urlpath);
  25.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  26.         conn.setRequestMethod("GET");
  27.         conn.setConnectTimeout(6 * 1000);
  28.         if (conn.getResponseCode() == 200) {
  29.             InputStream inputStream = conn.getInputStream();
  30.             byte[] data = readStream(inputStream);
  31.             return new String(data, encoding);
  32.         }
  33.         return null;
  34.     }
  35.  
  36.     public static byte[] readStream(InputStream inputStream) throws Exception {
  37.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  38.         byte[] buffer = new byte[1024];
  39.         int len = -1;
  40.         while ((len = inputStream.read(buffer)) != -1) {
  41.             outputStream.write(buffer, 0, len);
  42.         }
  43.         outputStream.close();
  44.         inputStream.close();
  45.         return outputStream.toByteArray();
  46.     }
  47. }

SaxTool

Java代码

  1. package org.wp.net.utils;
  2.  
  3. import java.io.InputStream;
  4. import javax.xml.parsers.SAXParser;
  5. import javax.xml.parsers.SAXParserFactory;
  6. import org.wp.domain.Resource;
  7.  
  8. public class SaxTool {
  9.  
  10.     public static Resource readXml(InputStream inStream) throws Exception {
  11.  
  12.         // 得到SAX解析器的工厂实例
  13.         SAXParserFactory spf = SAXParserFactory.newInstance();
  14.         // 从SAX工厂实例中获得SAX解析器
  15.         SAXParser saxParser = spf.newSAXParser();
  16.  
  17.         XMLContentHandler handler = new XMLContentHandler();
  18.         saxParser.parse(inStream, handler);
  19.         inStream.close();
  20.         return handler.getResource();
  21.     }
  22. }

XMLContentHandler

Java代码

  1. package org.wp.net.utils;
  2.  
  3. import java.util.ArrayList;
  4. import org.wp.domain.Item;
  5. import org.wp.domain.Resource;
  6. import org.xml.sax.Attributes;
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.helpers.DefaultHandler;
  9.  
  10. public class XMLContentHandler extends DefaultHandler {
  11.  
  12.     private Resource resource;
  13.  
  14.     @Override
  15.     public void startElement(String uri, String localName, String qName,
  16.             Attributes attributes) throws SAXException {
  17.         if ("R".equals(localName)) {
  18.             resource = new Resource(attributes.getValue("", "CN"), attributes
  19.                     .getValue("", "P"));
  20.             resource.setItems(new ArrayList<Item>());
  21.         } else if ("I".equals(localName) && resource != null) {
  22.             resource.getItems().add(
  23.                     new Item(new Integer(attributes.getValue("", "ID")),
  24.                             attributes.getValue("", "N"), attributes.getValue(
  25.                                     "", "CN")));
  26.         }
  27.     }
  28.  
  29.     public Resource getResource() {
  30.         return resource;
  31.     }
  32. }

org.wp.domain

Item

Java代码

  1. package org.wp.domain;
  2.  
  3. public class Item {
  4.     private Integer id;
  5.     private String name;
  6.     private String cn;
  7.  
  8.     public Item() {
  9.  
  10.     }
  11.  
  12.     public Item(Integer id, String name, String cn) {
  13.         this.id = id;
  14.         this.name = name;
  15.         this.cn = cn;
  16.     }
  17.  
  18.     public Integer getId() {
  19.         return id;
  20.     }
  21.  
  22.     public void setId(Integer id) {
  23.         this.id = id;
  24.     }
  25.  
  26.     public String getName() {
  27.         return name;
  28.     }
  29.  
  30.     public void setName(String name) {
  31.         this.name = name;
  32.     }
  33.  
  34.     public String getCn() {
  35.         return cn;
  36.     }
  37.  
  38.     public void setCn(String cn) {
  39.         this.cn = cn;
  40.     }
  41. }

Resource

Java代码

  1. package org.wp.domain;
  2.  
  3. import java.util.List;
  4.  
  5. public class Resource {
  6.     private String cn;
  7.     private String p;
  8.     private List<Item> items;
  9.  
  10.     public Resource() {
  11.  
  12.     }
  13.  
  14.     public Resource(String cn, String p) {
  15.         this.cn = cn;
  16.         this.p = p;
  17.     }
  18.  
  19.     public String getCn() {
  20.         return cn;
  21.     }
  22.  
  23.     public void setCn(String cn) {
  24.         this.cn = cn;
  25.     }
  26.  
  27.     public String getP() {
  28.         return p;
  29.     }
  30.  
  31.     public void setP(String p) {
  32.         this.p = p;
  33.     }
  34.  
  35.     public List<Item> getItems() {
  36.         return items;
  37.     }
  38.  
  39.     public void setItems(List<Item> items) {
  40.         this.items = items;
  41.     }
  42.  
  43.     @Override
  44.     public String toString() {
  45.         return "cn=" + cn + ",p=" + p;
  46.     }
  47. }

AndroidManifest.

代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android=""
  3.     package="org.wp.activity" android:versionCode="1" android:versionName="1.0">
  4.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  5.         <uses-library android:name="android.test.runner" />
  6.         <activity android:name=".XmlActivity" android:label="@string/app_name">
  7.             <intent-filter>
  8.                 <action android:name="android.intent.action.MAIN" />
  9.                 <category android:name="android.intent.category.LAUNCHER" />
  10.             </intent-filter>
  11.         </activity>
  12.     </application>
  13.     <uses-sdk android:minSdkVersion="7" />
  14.     <uses-permission android:name="android.permission.INTERNET" />
  15.     <!-- 在SDCard中创建与删除文件权限 -->
  16.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  17.     <!-- 往SDCard中写入数据权限 -->
  18.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  19.     <instrumentation android:name="android.test.InstrumentationTestRunner"
  20.         android:targetPackage="org.wp.activity" android:label="Tests for My App" />
  21. </manifest>

阅读(1218) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~