开发者社区> 问答> 正文

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

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

展开
收起
爱吃鱼的程序员 2020-06-09 16:13:31 793 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    这个是什么鬼?

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

    难道是你服务器路径?

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

    importjava.io.IOException;

    importnet.sf.json.JSONObject;

    importorg.apache.http.HttpResponse;
    importorg.apache.http.client.ClientProtocolException;
    importorg.apache.http.client.HttpClient;
    importorg.apache.http.client.methods.HttpPost;
    importorg.apache.http.entity.StringEntity;
    importorg.apache.http.impl.client.DefaultHttpClient;

    publicclassdda{
       publicstaticvoidmain(String[]args){
          
          try{
             HttpClienthttpclient=newDefaultHttpClient();
             
             Stringuri="http://localhost:8082/proxyWeb/testServlet";
    //         Stringuri="http://193.168.1.102:8080/project/answerOfQuestion/list.xml";
             
             System.out.println("bbbbbbbb");
             
             HttpPosthttppost=newHttpPost(uri);
             
             //添加http头信息
             httppost.addHeader("Authorization","yourtoken");//认证token
             httppost.addHeader("Content-Type","application/json");
             httppost.addHeader("User-Agent","imgfornote");
             
             //httppost的json数据格式:{"name":"yourname","parentId":
             //"id_of_parent"}
             System.out.println("test111111");
             JSONObjectobj=newJSONObject();
             obj.put("name","yourname");
             obj.put("parentId","yourparentid");
             httppost.setEntity(newStringEntity(obj.toString()));
             System.out.println("bbbbbbbbb");
             System.out.println("eeeeeeeeee");
             HttpResponseresponse;
             System.out.println("rrrrrrrr");
             response=httpclient.execute(httppost);
             System.out.println("mmmmmmmmm");
             //检验状态码,如果成功接收数据
             intcode=response.getStatusLine().getStatusCode();
             System.out.println(code+"qqqqqqqq");
             if(code==200){
                //Stringrev=
                //EntityUtils.toString(response.getEntity());//返回json格式:{"id":
                //"27JpL~j4vsL0LX00E00005","version":"abc"}
                //System.out.println(rev+"11111111");
                //obj=newJSONObject(rev);
                //
                //Stringid=obj.getString("id");
                //System.out.println(rev+"222222222");
                //Stringversion=obj.getString("version");
                //System.out.println(rev+"333333333");
                System.out.println("wawawawa");
             }
          }
          catch(ClientProtocolExceptione){
          }
          catch(IOExceptione){
          }
          catch(Exceptione){
          }
       }
    }

       <servlet>
          <description></description>
          <display-name>testServlet</display-name>
          <servlet-name>testServlet</servlet-name>
          <servlet-class>com.proxy.test.testServlet</servlet-class>
        </servlet>
        <servlet-mapping>
          <servlet-name>testServlet</servlet-name>
          <url-pattern>/testServlet</url-pattern>
        </servlet-mapping>packagecom.proxy.test;

    importjava.io.BufferedReader;
    importjava.io.IOException;
    importjava.io.InputStream;
    importjava.io.InputStreamReader;
    importjava.net.URLDecoder;

    importjavax.servlet.ServletException;
    importjavax.servlet.ServletOutputStream;
    importjavax.servlet.http.HttpServlet;
    importjavax.servlet.http.HttpServletRequest;
    importjavax.servlet.http.HttpServletResponse;

    importnet.sf.json.JSONObject;

    importorg.apache.http.HttpEntity;
    importorg.apache.http.HttpResponse;
    importorg.apache.http.HttpStatus;
    importorg.apache.http.StatusLine;
    importorg.apache.http.client.ClientProtocolException;
    importorg.apache.http.client.HttpClient;
    importorg.apache.http.client.methods.HttpPost;
    importorg.apache.http.entity.StringEntity;
    importorg.apache.http.impl.client.DefaultHttpClient;
    importorg.apache.http.params.HttpConnectionParams;
    importorg.apache.http.protocol.HTTP;
    importorg.apache.log4j.Logger;
     
    publicclasstestServletextendsHttpServlet{

       privatestaticfinalintConnectionTimeout=10000;
       privatestaticfinalintSoTimeout=10000;
       
       /**
        *<p>Discription:[序列号]</p>
        */
       privatestaticfinallongserialVersionUID=7841624938360477690L;
       
       /**日志*/

       protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
               throwsServletException,IOException{
           doPost(request,response);
       }

       protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
               throwsServletException,IOException{
          
          ServletOutputStreamout=response.getOutputStream();
          
           //读取请求内容
           BufferedReaderbr=newBufferedReader(newInputStreamReader(request.getInputStream()));
           Stringline=null;
           StringBuildersb=newStringBuilder();
           while((line=br.readLine())!=null){
               sb.append(line);
           }
           System.out.println(sb.toString());
       }
     

    }

    测试没问题,webxml里面配置路径
    我的电脑上边没装服务器,前辈的代码是不是需要服务器非常感谢感谢
    2020-06-09 16:13:49
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS计算与存储分离架构实践 立即下载