1. 乱码的问题
关于乱码的问题可以在服务器端设置接收和返回的数据编码格式,参考另一篇文章http://blog.csdn.net/dod_jdi/article/details/68938615
2. 如何传递对象
Javb可以使用JSONObject和JSONArray来处理对象及对象数组的传递问题。使用方法很简单,调用相关的静态方法就行,如数组:
JSONArray jsonArray = JSONArray.fromArray(对象数组);
PrintWriter writer = response.getWriter();
writer.print(jsonArray);
可以得到一个可以格式化的string来进行传递,解析时使用:
String response = request.sendRequest(url,params);
JSONArray jsonArray = JSONArray.fromObject(response);
List<类> 类= JSONArray.toList(jsonArray,类.class);
但是这里有个关于源数据为空的坑,使用JSONArray 如果原始数据为空那么解析得到的是中括号“[]”,使用JSONObject原始数据为空的话response 会是<!doctype html><html lang="en"><head><title>HTTP Status 500 – Internal Server Error</title><style type="text/css">
巴拉巴拉的东西,直接进行解析都会报错,所以解析之前都要先进行相关的判断
另外,Android端对应的类是Gson,不能使用net.sf.json进行解析