在上获取资源
org.wp.activity
GetXmlTest
Java代码
- package org.wp.activity;
-
-
import java.net.URLEncoder;
-
import java.io.ByteArrayInputStream;
-
import java.io.File;
-
import java.io.FileOutputStream;
-
import java.io.InputStream;
-
import java.io.OutputStreamWriter;
-
import org.wp.domain.Resource;
-
import org.wp.net.utils.NetTool;
-
import org.wp.net.utils.SaxTool;
-
import android.os.Environment;
-
import android.test.AndroidTestCase;
-
import android.util.Log;
-
-
public class GetXmlTest extends AndroidTestCase {
-
-
private static final String TAG = "GetXmlTest";
-
-
public void testGetXml() throws Exception {
-
// 如果参数是中文必须进行Url编码
-
// URLEncoder.encode("http://", "UTF-8");
-
String path = "";
-
String xml = NetTool.getTextContent(path, "UTF-8");
-
FileOutputStream outputStream = new FileOutputStream(new File(
-
Environment.getExternalStorageDirectory(), "test.xml"));
-
OutputStreamWriter writer = new OutputStreamWriter(outputStream,
-
"UTF-8");
-
writer.write(xml);
-
writer.flush();
-
writer.close();
-
outputStream.close();
-
Log.i(TAG, xml);
-
}
-
-
public void testGetXmlAndParser1() throws Exception {
-
String path = "";
-
String xml = NetTool.getTextContent(path, "UTF-8");
-
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
-
Resource resource = SaxTool.readXml(inputStream);
-
Log.i(TAG, resource.toString());
-
}
-
-
public void testGetXmlAndParser2() throws Exception {
-
String path = "";
-
InputStream inputStream = NetTool.getContent(path, "UTF-8");
-
if (inputStream != null) {
-
Resource resource = SaxTool.readXml(inputStream);
-
Log.i(TAG, resource.toString());
-
} else {
-
Log.i(TAG, "get url content error");
-
}
-
}
-
}
org.wp.net.utils
NetTool
Java代码
- package org.wp.net.utils;
-
-
import java.io.ByteArrayOutputStream;
-
import java.io.InputStream;
-
import java.net.HttpURLConnection;
-
import java.net.URL;
-
-
public class NetTool {
-
-
public static InputStream getContent(String urlpath, String encoding)
-
throws Exception {
-
URL url = new URL(urlpath);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setRequestMethod("GET");
-
conn.setConnectTimeout(6 * 1000);
-
if (conn.getResponseCode() == 200) {
-
return conn.getInputStream();
-
}
-
return null;
-
}
-
-
public static String getTextContent(String urlpath, String encoding)
-
throws Exception {
-
URL url = new URL(urlpath);
-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-
conn.setRequestMethod("GET");
-
conn.setConnectTimeout(6 * 1000);
-
if (conn.getResponseCode() == 200) {
-
InputStream inputStream = conn.getInputStream();
-
byte[] data = readStream(inputStream);
-
return new String(data, encoding);
-
}
-
return null;
-
}
-
-
public static byte[] readStream(InputStream inputStream) throws Exception {
-
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-
byte[] buffer = new byte[1024];
-
int len = -1;
-
while ((len = inputStream.read(buffer)) != -1) {
-
outputStream.write(buffer, 0, len);
-
}
-
outputStream.close();
-
inputStream.close();
-
return outputStream.toByteArray();
-
}
-
}
SaxTool
Java代码
- package org.wp.net.utils;
-
-
import java.io.InputStream;
-
import javax.xml.parsers.SAXParser;
-
import javax.xml.parsers.SAXParserFactory;
-
import org.wp.domain.Resource;
-
-
public class SaxTool {
-
-
public static Resource readXml(InputStream inStream) throws Exception {
-
-
// 得到SAX解析器的工厂实例
-
SAXParserFactory spf = SAXParserFactory.newInstance();
-
// 从SAX工厂实例中获得SAX解析器
-
SAXParser saxParser = spf.newSAXParser();
-
-
XMLContentHandler handler = new XMLContentHandler();
-
saxParser.parse(inStream, handler);
-
inStream.close();
-
return handler.getResource();
-
}
-
}
XMLContentHandler
Java代码
- package org.wp.net.utils;
-
-
import java.util.ArrayList;
-
import org.wp.domain.Item;
-
import org.wp.domain.Resource;
-
import org.xml.sax.Attributes;
-
import org.xml.sax.SAXException;
-
import org.xml.sax.helpers.DefaultHandler;
-
-
public class XMLContentHandler extends DefaultHandler {
-
-
private Resource resource;
-
-
@Override
-
public void startElement(String uri, String localName, String qName,
-
Attributes attributes) throws SAXException {
-
if ("R".equals(localName)) {
-
resource = new Resource(attributes.getValue("", "CN"), attributes
-
.getValue("", "P"));
-
resource.setItems(new ArrayList<Item>());
-
} else if ("I".equals(localName) && resource != null) {
-
resource.getItems().add(
-
new Item(new Integer(attributes.getValue("", "ID")),
-
attributes.getValue("", "N"), attributes.getValue(
-
"", "CN")));
-
}
-
}
-
-
public Resource getResource() {
-
return resource;
-
}
-
}
org.wp.domain
Item
Java代码
- package org.wp.domain;
-
-
public class Item {
-
private Integer id;
-
private String name;
-
private String cn;
-
-
public Item() {
-
-
}
-
-
public Item(Integer id, String name, String cn) {
-
this.id = id;
-
this.name = name;
-
this.cn = cn;
-
}
-
-
public Integer getId() {
-
return id;
-
}
-
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
-
public String getName() {
-
return name;
-
}
-
-
public void setName(String name) {
-
this.name = name;
-
}
-
-
public String getCn() {
-
return cn;
-
}
-
-
public void setCn(String cn) {
-
this.cn = cn;
-
}
-
}
Resource
Java代码
- package org.wp.domain;
-
-
import java.util.List;
-
-
public class Resource {
-
private String cn;
-
private String p;
-
private List<Item> items;
-
-
public Resource() {
-
-
}
-
-
public Resource(String cn, String p) {
-
this.cn = cn;
-
this.p = p;
-
}
-
-
public String getCn() {
-
return cn;
-
}
-
-
public void setCn(String cn) {
-
this.cn = cn;
-
}
-
-
public String getP() {
-
return p;
-
}
-
-
public void setP(String p) {
-
this.p = p;
-
}
-
-
public List<Item> getItems() {
-
return items;
-
}
-
-
public void setItems(List<Item> items) {
-
this.items = items;
-
}
-
-
@Override
-
public String toString() {
-
return "cn=" + cn + ",p=" + p;
-
}
-
}
AndroidManifest.
代码
- <?xml version="1.0" encoding="utf-8"?>
-
<manifest xmlns:android=""
-
package="org.wp.activity" android:versionCode="1" android:versionName="1.0">
-
<application android:icon="@drawable/icon" android:label="@string/app_name">
-
<uses-library android:name="android.test.runner" />
-
<activity android:name=".XmlActivity" android:label="@string/app_name">
-
<intent-filter>
-
<action android:name="android.intent.action.MAIN" />
-
<category android:name="android.intent.category.LAUNCHER" />
-
</intent-filter>
-
</activity>
-
</application>
-
<uses-sdk android:minSdkVersion="7" />
-
<uses-permission android:name="android.permission.INTERNET" />
-
<!-- 在SDCard中创建与删除文件权限 -->
-
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
-
<!-- 往SDCard中写入数据权限 -->
-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
<instrumentation android:name="android.test.InstrumentationTestRunner"
-
android:targetPackage="org.wp.activity" android:label="Tests for My App" />
-
</manifest>
阅读(1247) | 评论(0) | 转发(0) |