zoukankan      html  css  js  c++  java
  • 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();
       }

  • 相关阅读:
    C语言-if语句
    C语言-表达式
    C语言-基础
    Java for LeetCode 187 Repeated DNA Sequences
    Java for LeetCode 179 Largest Number
    Java for LeetCode 174 Dungeon Game
    Java for LeetCode 173 Binary Search Tree Iterator
    Java for LeetCode 172 Factorial Trailing Zeroes
    Java for LeetCode 171 Excel Sheet Column Number
    Java for LeetCode 169 Majority Element
  • 原文地址:https://www.cnblogs.com/zxcnn/p/5080927.html
Copyright © 2011-2022 走看看