辍耕录 2018-05-15
代码:
1 protected void onHandler(HttpServletRequest request, HttpServletResponse response, PrintWriter printWriter) { 2 Map<String, Object> map = new HashMap<String, Object>(); 3 4 try { 5 String responseS = onResponse(request, response); 6 map.put("data", responseS);// 字符串、json、url 7 map.put("error", 1);// 1的时候代表服务端执行成功 8 map.put("msg", "OK"); 9 } catch (Exception e) { 10 map.put("error", "-1");// error是-1的时候代表服务端有问题 11 map.put("msg", "服务端操作数据库出现异常"); 12 map.put("data", ""); 13 } 14 15 JSONObject jsonObject = (JSONObject) JSON.toJSON(map); 16 printWriter.write(jsonObject.toJSONString()); 17 } 18 19 protected String onResponse(HttpServletRequest request, HttpServletResponse response) throws Exception { 20 Map<String, String> map = new HashMap(); 21 map.put("userName", "甘传谱"); 22 map.put("password", "123456"); 23 return JSON.toJSONString(map); 24 }
json:
{"msg":"OK","data":"{\"password\":\"123456\",\"userName\":\"甘传谱\"}","error":}
代码:
public class UserInfo { @JSONField(name = "userName") private String userName; @JSONField(name = "password") private String password; public UserInfo() { super(); } public UserInfo(String userName, String password) { super(); this.userName = userName; this.password = password; }
String url = "http://172.16.10.240:8080/noHttpServer/UserInfoServlet"; Request<BaseBean> request = new BaseRequest<BaseBean>(url, BaseBean.class); request(, request, callBack, false, true); ==============回调======================== BaseBean baseBean = response.get(); if (baseBean.isSuccess()) { UserInfo content = baseBean.parseData(UserInfo.class); String s = "用户名:" + content.getUserName() + ";密码:" + content.getPassword(); mTvResult.setText(s); } else { mTvResult.setText("业务处理层数据失败"); }
代码:
1 protected void onHandler(HttpServletRequest request, HttpServletResponse response, PrintWriter printWriter) { 2 Map<String, Object> map = new HashMap<String, Object>(); 3 4 try { 5 String responseS = onResponse(request, response); 6 map.put("data", responseS);// 字符串、json、url 7 map.put("error", 1);// 1的时候代表服务端执行成功 8 map.put("msg", "OK"); 9 } catch (Exception e) { 10 map.put("error", "-1");// error是-1的时候代表服务端有问题 11 map.put("msg", "服务端操作数据库出现异常"); 12 map.put("data", ""); 13 } 14 15 JSONObject jsonObject = (JSONObject) JSON.toJSON(map); 16 printWriter.write(jsonObject.toJSONString()); 17 } 18 19 protected String onResponse(HttpServletRequest request, HttpServletResponse response) throws Exception { 20 Map<String, Object> map = new HashMap(); 21 List<String> books = new ArrayList(); 22 books.add("Java"); 23 books.add("Android"); 24 books.add("iOS"); 25 books.add("C#"); 26 books.add(".NET"); 27 books.add("PHP"); 28 map.put("books", books); 29 return JSON.toJSONString(map); 30 }
json:
{"msg":"OK","data":"{\"books\":[\"Java\",\"Android\",\"iOS\",\"C#\",\".NET\",\"PHP\"]}","error":}
代码:
public class BooksInfo { @JSONField(name = "books") private List<String> list; public BooksInfo() { super(); } public BooksInfo(List<String> list) { super(); this.list = list; }
String url = "http://172.16.10.240:8080/noHttpServer/BookServlet"; Request<BaseBean> request = new BaseRequest<BaseBean>(url, BaseBean.class); request(, request, callBack, false, true); ==========回调================================== BaseBean baseBean = response.get(); if (baseBean.isSuccess()) { BooksInfo content = baseBean.parseData(BooksInfo.class); List<String> list =content.getList(); String s=""; for(int i=;i<list.size();i++){ s+= "书籍名字" +i + ":" + list.get(i)+"\n"; } mTvResult.setText(s); } else { mTvResult.setText("业务处理层数据失败"); }
完整代码:https://github.com/ganchuanpu/StarHttp