我想以post方式向服务器发送json数据,为什么总是失败? 求大神指点,谢谢了

发布于 2021-11-29 06:48:57 字数 6034 浏览 761 评论 7

package testinterface;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;


public class dda {
 public static void main(String[] args) {
   
      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) { 
          }
     }
 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();
     }


}
 
 
 报错信息如下:

If you would like to submit a bug report, please visit:
#   http://bugreport.java.com/bugreport/crash.jsp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔少女心 2021-12-01 20:58:06

非常感谢感谢

拍不死你 2021-12-01 20:48:50

我的电脑上边没装服务器,前辈的代码是不是需要服务器

反目相谮 2021-12-01 20:03:09

测试 没问题,web xml里面配置路径

倚栏听风 2021-12-01 15:18:24

package com.proxy.test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.URLDecoder;

import javax.servlet.ServletException;

import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.HttpStatus;

import org.apache.http.StatusLine;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.params.HttpConnectionParams;

import org.apache.http.protocol.HTTP;

import org.apache.log4j.Logger;

 

public class testServlet extends HttpServlet {

    private static final int ConnectionTimeout = 10000;

    private static final int SoTimeout = 10000;

    

    /**

     * <p>Discription:[序列号]</p>

     */

    private static final long serialVersionUID = 7841624938360477690L;

    

    /** 日志 */

    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        doPost(request, response);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {

        

        ServletOutputStream out = response.getOutputStream();

        

        // 读取请求内容

        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));

        String line = null;

        StringBuilder sb = new StringBuilder();

        while((line = br.readLine())!=null){

            sb.append(line);

        }

        System.out.println(sb.toString());

    }

 

}

霞映澄塘 2021-12-01 15:12:14

package com.proxy.test;

import java.io.IOException;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.DefaultHttpClient;

public class dda {

    public static void main(String[] args) {

        

        try {

            HttpClient httpclient = new DefaultHttpClient();

            

            String uri = "http://localhost:8082/proxyWeb/testServlet";

//            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) {

        }

    }

}

青萝楚歌 2021-12-01 14:10:13

前辈能否发您的安卓项目源码发我一份,我也说不清自己那块出问题了

奢望 2021-12-01 10:29:00

这个是什么鬼?

http://193.168.1.102:8080/project/answerOfQuestion/list.xml

难道是你服务器路径?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文