我想以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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
非常感谢感谢
我的电脑上边没装服务器,前辈的代码是不是需要服务器
测试 没问题,web xml里面配置路径
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());
}
}
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) {
}
}
}
前辈能否发您的安卓项目源码发我一份,我也说不清自己那块出问题了
这个是什么鬼?
http://193.168.1.102:8080/project/answerOfQuestion/list.xml
难道是你服务器路径?