手机从电脑上得到的是一个xml文件,这个文件要被解析,解析一般有两种方法,一种是xml解析,一种是json解析。xml大家都知道,json是什么呢,其实它是类似于数组方式存储的字符串,也就是一段文本。
一般的错误都是URL地址写错了导致连接出现问题,所以这里要注意。其次一定要记得配权限。
下面是案例:
package com.icss.test_pc_connection_mobile;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xml.sax.SAXException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.util.Log;
import android.util.Xml;
public class XmlParserUtil {
/**
* SAX解析
*/
public static List<Person> getListFromWebBySax(InputStream inputStream)
throws ParserConfigurationException, SAXException, IOException {
List<Person> list = null;
// 业务
// 1. 创建SAX解析器工厂
SAXParserFactory spf = SAXParserFactory.newInstance();
// 2. 通过工厂拿到解析 器
SAXParser sp = spf.newSAXParser();
// 3. 解析 器加流及handler对象来解析
PersonHandler sxh = new PersonHandler();
sp.parse(inputStream, sxh);
// 5. handler中取出list
list = sxh.getList();
// 4. 关闭流
inputStream.close();
return list;
}
/**
* json解析
*
* @throws JSONException
*/
public static List<Person> getListFromWebByJson(InputStream inputStream)
throws ParserConfigurationException, SAXException, IOException,
XmlPullParserException, JSONException {
List<Person> list = null;
Person s = null;
// 从流中读取数据
byte[] b = new byte[1024];
// du dao liu
inputStream.read(b);
// ba liu change String
String str = new String(b, "utf-8");
JSONArray ja = new JSONArray(str);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
s.setId(jo.getInt("id"));
s.setName(jo.getString("name"));
s.setAge(jo.getInt("age"));
list.add(s);
}
inputStream.close();
return list;
}
/**
* pull解析
*/
public static List<Person> getListFromWebByPull(InputStream inputStream)
throws ParserConfigurationException, SAXException, IOException,
XmlPullParserException {
List<Person> list = null;
Person s = null;
// 一种,直接pull
// XmlPullParserFactory f=XmlPullParserFactory.newInstance();
// XmlPullParser p=f.newPullParser(); //pull解析器
// 二种, android对pull的支持
XmlPullParser p = Xml.newPullParser(); // 通过android的pull支持创建一个pull解析器
p.setInput(inputStream, "utf-8");
int eventType = p.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
Log.i("localName", "" + p.getName());
// 每一个节点业务
switch (eventType) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList<Person>();
break;
case XmlPullParser.START_TAG:
if ("person".equals(p.getName())) {
s = new Person();
s.setId(new Integer(p.getAttributeValue(null, "id")));
} else if ("name".equals(p.getName())) {
if (s != null) {
s.setName(p.nextText());
}
} else if ("age".equals(p.getName())) {
if (s != null) {
s.setAge(new Integer(p.nextText()));
}
}
break;
case XmlPullParser.END_TAG:
if ("person".equals(p.getName()) && s != null) {
list.add(s);
}
break;
}
eventType = p.next(); // 到下一个节点
}
return list;
}
}
上面是两种解析方式,其他代码在附件中。