編輯:關於Android編程
Android跟服務器交互數據,有時數據量大時,就需要以xml形式的交互數據。這裡來實現服務器給Android客戶端發送xml數據,Android客戶端解析。
服務器端我使用dom4j第三方包來組織xml數據,大家可自行百度下載。而Android客戶端則使用 XmlPullParser來解析xml數據。
服務器端代碼:
[java] package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class getSms extends HttpServlet {
public getSms() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8" );
request.setCharacterEncoding("UTF-8");
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
public class getSms extends HttpServlet {
public getSms() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8" );
request.setCharacterEncoding("UTF-8"); [java] view plaincopyprint?Document document = DocumentHelper.createDocument();
Element persons = DocumentHelper.createElement("persons");
document.add(persons);
Document document = DocumentHelper.createDocument();
Element persons = DocumentHelper.createElement("persons");
document.add(persons); [java] view plaincopyprint?persons.addElement("address").addText("南京");
persons.addElement("id").addAttribute(1);
persons.addElement("thread_id").addText(2);
persons.addElement("date").addText("2013");
persons.addElement("status").addText(-1);
persons.addElement("type").addText(0);
persons.addElement("body").addText("測試");
persons.addElement("address").addText("南京");
persons.addElement("id").addAttribute(1);
persons.addElement("thread_id").addText(2);
persons.addElement("date").addText("2013");
persons.addElement("status").addText(-1);
persons.addElement("type").addText(0);
persons.addElement("body").addText("測試"); [java] view plaincopyprint? String xml = document.asXML(); //用dom4j組織一個XML字符串
response.setContentType("text/xml; charset=UTF-8"); //設置返回值的類型
response.getOutputStream().write(xml.getBytes("UTF-8")); //設置返回值
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException {
// Put your code here
}
String xml = document.asXML(); //用dom4j組織一個XML字符串
response.setContentType("text/xml; charset=UTF-8"); //設置返回值的類型
response.getOutputStream().write(xml.getBytes("UTF-8")); //設置返回值
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
public void init() throws ServletException {
// Put your code here
}
}
Android客戶端:
[java] String url="http://10.0.2.2:8080/sms_server/servlet/getSms";
try{
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
Log.i("start_document", "start_document");
switch (event) {
case XmlPullParser.START_TAG:
if ("address".equals(parser.getName())) {
String address=parser.nextText();
}
if ("id".equals(parser.getName())) {
int id=parser.getAttributeValue(0);
}
if (("thread_id").equals(parser.getName())) {
String thread_id=parser.nextText();
}
if ("date".equals(parser.getName())) {
String date=parser.nextText();
}
if (("status").equals(parser.getName())) {
String status=parser.nextText();
}
if ("type".equals(parser.getName())) {
String type=parser.nextText();
}
if ("body".equals(parser.getName())) {
String body=parser.nextText();
}
break;
case XmlPullParser.END_TAG:
break;
}
event = parser.next();
}
}
}catch(Exception e){
}
String url="http://10.0.2.2:8080/sms_server/servlet/getSms";
try{
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setRequestMethod("GET");
if (conn.getResponseCode() == 200) {
InputStream in = conn.getInputStream();
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, "UTF-8");
int event = parser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
Log.i("start_document", "start_document");
switch (event) {
case XmlPullParser.START_TAG:
if ("address".equals(parser.getName())) {
String address=parser.nextText();
}
if ("id".equals(parser.getName())) {
int id=parser.getAttributeValue(0);
}
if (("thread_id").equals(parser.getName())) {
String thread_id=parser.nextText();
}
if ("date".equals(parser.getName())) {
String date=parser.nextText();
}
if (("status").equals(parser.getName())) {
String status=parser.nextText();
}
if ("type".equals(parser.getName())) {
String type=parser.nextText();
}
if ("body".equals(parser.getName())) {
String body=parser.nextText();
}
break;
case XmlPullParser.END_TAG:
break;
}
event = parser.next();
}
}
}catch(Exception e){
}
吹在前面的話:ListView下刷新刷功能相信從事Android開發的猿友們並不陌生,包括現在Google親兒子SwipeRefreshLayout實現效果在一些APP上
特點:改變的是對象的實際屬性 不僅可以應用於View, 有getter和setter方法的都可以在xml中定義放在res\animator中如:animator_alph
本文實例講述了Android中ViewFlipper的使用及設置動畫效果。分享給大家供大家參考,具體如下:說到左右滑動,其實實現左右滑動的方式很多,有ViewPaer,自
接觸了百度地圖開發平台半個月了,這2天試著模仿了微信給好友發送位置功能,對百度地圖的操作能力又上了一個台階(如果需要完整demo,請評論留下郵箱) 我在實現這個