博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android网络通讯数据封装之 json
阅读量:5162 次
发布时间:2019-06-13

本文共 4484 字,大约阅读时间需要 14 分钟。

Demo程序包括客户端和服务端

客户端按json数据格式封装数据传至服务端。

服务端为简单的servlet程序,负责接收客户端传到json数据,然后按原数据返回客户端.

实例代码如下:

public static String cmdLogIn() {   String urlString = "http://192.168.8.75:89/webroot/jsontest";   HttpPost request = new HttpPost(urlString);      try{    //json数据格式{"data":{"spring":"yes","java":"ok"},                   "head": {
"password":"123456","name":"rarnu"}} //封装代码 JSONObject headParam = new JSONObject(); headParam.put("name", "yihu"); headParam.put("password", "123456"); JSONObject dataParam = new JSONObject(); dataParam.put("java", "ok"); dataParam.put("spring", "yes"); JSONObject paramss = new JSONObject(); paramss.put("head", headParam); paramss.put("data", dataParam);
//json数据格式{
"data":[{
"spring":"yes","java":"ok"}], "head":[{
"password":"123456","name":"yihu"},{
"arry1":"java","classid":"321546"}]} JSONArray headParams = new JSONArray(); //数组[0] JSONObject headParam1 = new JSONObject(); headParam1.put("name", "yihu"); headParam1.put("password", "123456"); //数组[1] JSONObject headParam2 = new JSONObject(); headParam2.put("arry1", "java"); headParam2.put("classid", "321546"); headParams.put(headParam1); headParams.put(headParam2); JSONArray dataParams = new JSONArray(); JSONObject dataParam = new JSONObject(); dataParam.put("java", "ok"); dataParam.put("spring", "yes"); dataParams.put(dataParam); JSONObject paramss = new JSONObject(); paramss.put("head", headParams); paramss.put("data", dataParams); Log.v("tag", "params.toString() >>> :"+paramss.toString()); // 绑定到请求 Entry StringEntity se = new StringEntity(paramss.toString()); request.setEntity(se); // 发送请求 HttpResponse httpResponse = new DefaultHttpClient().execute(request); int statusCode = httpResponse.getStatusLine().getStatusCode(); Log.v("tag", "statusCode <- :"+statusCode); if (statusCode == 200) { InputStream entityStream; try { entityStream = httpResponse.getEntity().getContent(); String jsonString = convertStreamToString(entityStream); Log.v("tag", "jsonString <- :"+jsonString); if ("".equals(jsonString)) return null; JSONObject json = new JSONObject(jsonString); JSONObject head = json.getJSONObject("head"); Log.v("TAG", "name >> :"+head.optString("name")); return json.optString("java"); }catch (Exception e) { } } }catch (Exception e) { } return null; } private static String convertStreamToString(InputStream is) { StringBuilder sb = new StringBuilder(); try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "UTF-8"), 8 * 1024); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "n"); } } catch (IOException e) { sb.delete(0, sb.length()); } finally { try { is.close(); } catch (IOException e) { } } return sb.toString(); }

服务端测试代码

public class LoadServlet extends HttpServlet { public void destroy() {  super.destroy(); }  public void doGet(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  StringBuffer messagebuffer = new StringBuffer();  javax.servlet.ServletInputStream in = request.getInputStream();  DataInputStream din = new DataInputStream(in);  int ch;  while ((ch = din.read()) != -1)   messagebuffer.append((char) ch);  din.close();    String message = messagebuffer.toString();  System.out.println("content...>>>:" + message);    ByteArrayOutputStream bout = new ByteArrayOutputStream();  DataOutputStream dout = new DataOutputStream(bout);  byte str[] = message.getBytes("UTF-8");  dout.write(str);  byte data[] = bout.toByteArray();  OutputStream out = response.getOutputStream();  out.write(data);  out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {  doGet(request, response); } public void init() throws ServletException { }}web.xml
jsontest
jsontest
jsonServlet
com.yihu.json.servlet.LoadServlet
jsonServlet
/jsontest

 

转载于:https://www.cnblogs.com/zhujiabin/p/5285973.html

你可能感兴趣的文章
基于wxPython的python代码统计工具
查看>>
淘宝JAVA中间件Diamond详解(一)---简介&快速使用
查看>>
Hadoop HBase概念学习系列之HBase里的宽表设计概念(表设计)(二十七)
查看>>
Kettle学习系列之Kettle能做什么?(三)
查看>>
Day03:Selenium,BeautifulSoup4
查看>>
awk变量
查看>>
mysql_对于DQL 的简单举例
查看>>
35. Search Insert Position(C++)
查看>>
[毕业生的商业软件开发之路]C#异常处理
查看>>
一些php文件函数
查看>>
有关快速幂取模
查看>>
Linux运维必备工具
查看>>
字符串的查找删除
查看>>
NOI2018垫底记
查看>>
快速切题 poj 1002 487-3279 按规则处理 模拟 难度:0
查看>>
Codeforces Round #277 (Div. 2)
查看>>
【更新】智能手机批量添加联系人
查看>>
NYOJ-128前缀式计算
查看>>
深入理解 JavaScript 事件循环(一)— event loop
查看>>
Hive(7)-基本查询语句
查看>>