android解析服务器传来的xml文件和json文件(一)

      用一个简单的例子来解析服务器端传来的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;
	}
}


getcitylistxml是将自己定义好的一个xml文件读到inputStream中然后通过缓冲流读成xml字符串,再将字符串传递给客户端来解析就ok了。

我的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服务器端就是这样。马上上客户端的代码。请见下一篇:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值