用一个简单的例子来解析服务器端传来的xml文件,首先要在服务器端建立一个网站。在网站中建立了一个servlet的类名字叫CityAction。它用来响应android端用post方法传来的请求。再建立一个CityDataSource类,里面的getCityList()方法返回给CityAction一个城市名的list,然后再CityAction封装成一个json字符串,传递给android客户端。json的jar包我会在前面的资源中给出来过,大家可以免费下载。代码如下:
package com.city.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONSerializer;
public class CityAction extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter writer=response.getWriter();
String type=request.getParameter("type");
if(type.equals("json")){
List<String> list=CityDataSource.getCityList();
Map<String, List<String>> map=new HashMap<String, List<String>>();
map.put("citys", list);
String jsonString=JSONSerializer.toJSON(map).toString();
writer.println(jsonString);
}else if(type.equals("xml")){
String jsonString=CityDataSource.getCityListXML();
writer.println(jsonString);
}
writer.flush();
writer.close();
}
}
jsonString 就是要传递给客户端的字符串。
package com.city.action;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
public class CityDataSource {
public static List<String> getCityList(){
List<String> list=new ArrayList<>();
list.add("北京");
list.add("上海");
list.add("南京");
list.add("成都");
list.add("天津");
return list;
}
public static String getCityListXML(){
//List<String> list=new ArrayList<>();
//把文件读到字节流中
InputStream inputStream=CityDataSource.class.getClassLoader()
.getResourceAsStream("com/city/action/citys.xml");
//在把字节流通过缓冲流读到字符串中
Reader reader=new InputStreamReader(inputStream,Charset.forName("utf-8"));
BufferedReader bufferedReader=new BufferedReader(reader);
String value="";
StringBuilder builder=new StringBuilder();
try {
while((value=bufferedReader.readLine())!=null){
builder.append(value);
}
//System.out.println(builder.toString());
return builder.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
if(bufferedReader!=null){
try {
bufferedReader.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
return null;
}
}
我的xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<citys>
<city>
<name>北京</name>
</city>
<city>
<name>上海</name>
</city>
<city>
<name>成都</name>
</city>
<city>
<name>河南</name>
</city>
<city>
<name>西藏</name>
</city>
</citys>
服务器端就完成了,在访问的时候只需
http://localhost:8080/xmljson/servlet/CityAction?type=json
http://localhost:8080/xmljson/servlet/CityAction?type=xml
通过type就可以知道你需要哪种格式的返回文件。
ok服务器端就是这样。马上上客户端的代码。请见下一篇: