发送解析XML

前端之家收集整理的这篇文章主要介绍了发送解析XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

// 用于创建一个XML文件的类

public class XmlHandler {
public String writeXml(ArrayList<String> dataList) {
StringWriter xmlWriter = new StringWriter();

try {
// 创建XmlSerializer
XmlPullParserFactory pullParserFactory = XmlPullParserFactory.newInstance();
XmlSerializer xmlSerializer = pullParserFactory.newSerializer();

xmlSerializer.setOutput(xmlWriter);

// 写xml文件
// <?xml version="1.0" encoding="UTF-8" standlone="yes" ?>
xmlSerializer.startDocument("UTF-8",true);

// <Feed number="25">
xmlSerializer.startTag("","Feed");
xmlSerializer.attribute("","number",String.valueOf(dataList.size()));
//<entry>
xmlSerializer.startTag("","entry");

// <title> value </title>
xmlSerializer.startTag("","title");
xmlSerializer.text(dataList.get(0));
xmlSerializer.endTag("","title");

// <telphone> value </telphone>
xmlSerializer.startTag("","telephone");
xmlSerializer.text(dataList.get(1));
xmlSerializer.endTag("","telephone");

// <helpData> value </ helpData>
xmlSerializer.startTag("","sendData");
xmlSerializer.text(dataList.get(2));
xmlSerializer.endTag("","sendData");

// <date> value </value>
xmlSerializer.startTag("","date");
xmlSerializer.text(dataList.get(3));
xmlSerializer.endTag("","date");

xmlSerializer.endTag("","entry");

xmlSerializer.endTag("","Feed");

xmlSerializer.endDocument();
}
catch(Exception ex) {
ex.printStackTrace();
}

// xml创建完成,返回数据
return xmlWriter.toString();
}

}

// 创建xml文件

String createXml() {

ArrayList<String> dataList = null;
dataList = new ArrayList<String>();

dataList.add(0,"");
TelephonyManager phoneManager = (TelephonyManager)
XmlSend.this.getSystemService(Context.TELEPHONY_SERVICE);

// 此处需要添加一个获取电话号码的服务

// <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
dataList.add(1,phoneManager.getLine1Number());
dataList.add(2,editText.getText().toString().trim());

// 获取系统当前时间
Time time = new Time("GMT+8");
time.setToNow();
String strCurTime = time.year + ":" + time.month + ":" + time.monthDay
+ " " + time.hour + ":" + time.minute;
dataList.add(3,strCurTime);
XmlHandler handler = new XmlHandler();

return handler.writeXml(dataList);
}

// 发送XML到服务器并解析返回结果

boolean sendXml(String xmlData) throws IOException,XmlPullParserException,URISyntaxException {

// 向服务器发送xml文件

// 里面还有些问题,现在也没有该了 URL url = new URL("..."); // 打开服务器 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); // 设置超时值 conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); // 设置发送字符串的编码格式 conn.setRequestProperty("Content-Type","text/xml; charset=UTF-8"); OutputStream outStream = conn.getOutputStream(); OutputStreamWriter outStreamWriter = new OutputStreamWriter(outStream); outStreamWriter.write(xmlData); outStreamWriter.close(); outStream.close(); // 等待服务器返回发送结果,解析xml数据查看是否发送成功 URI uri = new URI("..."); HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(uri); HttpResponse response = client.execute(get); InputStream inputStream = response.getEntity().getContent(); XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); factory.setNamespaceAware(true); XmlPullParser parser = factory.newPullParser(); parser.setInput(inputStream,"UTF-8"); for(int i = parser.getEventType(); i != XmlPullParser.END_DOCUMENT; i = parser.next()) { // 需要修改其xml的文件名 if(i == XmlPullParser.START_TAG && parser.getName().equals("title")) { Toast.makeText(XmlSend.this,"发送成功",Toast.LENGTH_SHORT); break; } } return true; }

原文链接:https://www.f2er.com/xml/298147.html

猜你在找的XML相关文章