• android客户端向java服务端post发送json


    android 端:

    private void HttpPostData() {  
          try { 

              HttpClient httpclient = new DefaultHttpClient();  
              
              String uri = "http://193.168.1.102:8080/project/answerOfQuestion/list.xml"; 
              
              System.out.println("bbbbbbbb");
              
              HttpPost httppost = new HttpPost(uri);   
          
              //添加http头信息 

              httppost.addHeader("Authorization", "your token"); //认证token 

              httppost.addHeader("Content-Type", "application/json"); 

              httppost.addHeader("User-Agent", "imgfornote");  
              
              //http post的json数据格式:  {"name": "your name","parentId": "id_of_parent"}  
              System.out.println("test111111");
              JSONObject obj = new JSONObject(); 

              obj.put("name", "your name"); 

              obj.put("parentId", "your parentid"); 

              httppost.setEntity(new StringEntity(obj.toString()));     
              System.out.println("bbbbbbbbb");
              System.out.println("eeeeeeeeee");
             HttpResponse response;  
             System.out.println("rrrrrrrr");
              response = httpclient.execute(httppost);  
              System.out.println("mmmmmmmmm");
              //检验状态码,如果成功接收数据  
              int code = response.getStatusLine().getStatusCode();  
              System.out.println(code+"qqqqqqqq");
              if (code == 200) {  

    //              String rev = EntityUtils.toString(response.getEntity());//返回json格式: {"id": "27JpL~j4vsL0LX00E00005","version": "abc"}         
    //              System.out.println(rev+"11111111");
    //              obj = new JSONObject(rev);  
    //
    //              String id = obj.getString("id");  
    //              System.out.println(rev+"222222222");
    //              String version = obj.getString("version");  
    //              System.out.println(rev+"333333333");
               System.out.println("wawawawa");
              } 

              } catch (ClientProtocolException e) {    

              } catch (IOException e) {    

              } catch (Exception e) {  

              } 

         }

    java 端:

    try {
       BufferedInputStream in = new BufferedInputStream(request.getInputStream());
          int i;
          char c;
          while ((i=in.read())!=-1) {
          c=(char)i;
          System.out.print(c);
          }
          System.out.println();
          System.out.println("test");
         }catch (Exception ex) {
       ex.printStackTrace();
       }

  • 相关阅读:
    [Linux] Linux文件系统目录描述简介
    面试题07 二叉树两节点的最低公共祖先 [树]
    [C++] Stack / queue / priority_queue
    c#中byte[]和string的转换
    smartassembly 使用方法
    关于 Expression Blend 4安装是出现的“意见安排重启您的计算机”的解决方法
    php图片压缩
    我的dota之路
    OO系统设计师之路设计模型系列(1)软件架构和软件框架[从老博客搬家至此]
    const用法详解
  • 原文地址:https://www.cnblogs.com/zxcnn/p/5080927.html
Copyright © 2020-2023  润新知