谁能帮小妹解决andriod以post方式请求rest接口遇到的问题 RESTURLpost,415 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看看这个: // JSONObject data, String url HttpPost request = new HttpPost(url); StringEntity entity; entity = new StringEntity(data.toString()); request.addHeader("Content-Type", "application/json"); request.setEntity(entity); // 发送请求 HttpResponse httpResponse = new DefaultHttpClient().execute(request); 首先,谢谢大哥的提示,参考上面的代码我改了一下,那个问题解决了,但是随即又出现另外一个问题,依然让我一头雾水。描述如下:我参考你提供的代码将发送Post请求飞方法改成public static String postRequest(String url, JSONObject jsonObject) throws Exception { // 创建HttpPost对象。 HttpPost post = new HttpPost(url); // 设置请求参数 StringEntity entity = new StringEntity(jsonObject.toString()); post.setEntity(entity); post.setHeader("Accept", "application/json"); post.setHeader("Content-Type", "application/json"); // 发送POST请求 HttpResponse httpResponse = httpClient.execute(post); // 如果服务器成功地返回响应 if (httpResponse.getStatusLine().getStatusCode() == 200) { // 获取服务器响应字符串 String result = EntityUtils.toString(httpResponse.getEntity()); return result; } return null; }现在httpResponse.getStatusLine().getStatusCode()返回状态200没有问题,有问题的是进入if语句后EntityUtils.toString(httpResponse.getEntity());却抛了异常。抛出的异常是:java.lang.IllegalStateException: Content has been consumed。我网上查了这个异常,原因是指httpResponse.getEntity()只能被读取一次。但是上面的代码中httpResponse.getEntity()只是被调了一次呀,为什么还会出现这个异常呢。不解中,希望得到指点。 public static JSONObject response2Json(HttpResponse httpresponse) throws IOException, JSONException { HttpEntity resultentity = httpresponse.getEntity(); InputStream inputstream = resultentity.getContent(); String resultstring = convertStreamToString(inputstream); inputstream.close(); Log.d(TAG, "result = " + resultstring); return new JSONObject(resultstring); } 代码应该没有问题的,唯一不一样的地方是我的代码中是new DefaultHttpClient(),而你的是httpClient,是一个保存下来的,多次调用时出现你描述的错误吧。你改过来再试一试。 楼主,我想问个关于REST服务的问题!我自己用VS2010搭建了一个简单的REST服务,里边有GET、POST、PUT和DELETE四种请求服务,通过POST请求调用REST服务的时候,传递参数用一个List来做,但是参数名称总是不对,不知道该怎么解决了,希望你能听懂,帮帮忙 Android开发 求一个功能的实现 请问这是什么错误,在android下遇到的问题 Mp3中OnResume怎样放回onServiceConnected对象 学 Android 有钱途吗? 求教 Android手机上怎么实现短信拦截? WebView点击网页文本输入框时不能弹出软键盘 这个错误啥意思呢。 想找一个公司开发一款手机游戏,只有一个构思,请问需要准备什么? 移动应用或者手机游戏现在服务端的架构一般都是怎么样的 用jquery mobile开发的安卓程序如何上传文件 cocos2d-x中的HelloCpp不能跳转activity,求解 这个界面是怎么实现
request.addHeader("Content-Type", "application/json");
request.setEntity(entity);
// 发送请求
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
我参考你提供的代码将发送Post请求飞方法改成
public static String postRequest(String url, JSONObject jsonObject)
throws Exception {
// 创建HttpPost对象。
HttpPost post = new HttpPost(url);
// 设置请求参数
StringEntity entity = new StringEntity(jsonObject.toString());
post.setEntity(entity);
post.setHeader("Accept", "application/json");
post.setHeader("Content-Type", "application/json");
// 发送POST请求
HttpResponse httpResponse = httpClient.execute(post);
// 如果服务器成功地返回响应
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 获取服务器响应字符串
String result = EntityUtils.toString(httpResponse.getEntity());
return result;
}
return null;
}
现在
httpResponse.getStatusLine().getStatusCode()返回状态200没有问题,有问题的是进入if语句后EntityUtils.toString(httpResponse.getEntity());却抛了异常。抛出的异常是:java.lang.IllegalStateException: Content has been consumed。
我网上查了这个异常,原因是指httpResponse.getEntity()只能被读取一次。但是上面的代码中httpResponse.getEntity()只是被调了一次呀,为什么还会出现这个异常呢。不解中,希望得到指点。
HttpEntity resultentity = httpresponse.getEntity();
InputStream inputstream = resultentity.getContent();
String resultstring = convertStreamToString(inputstream);
inputstream.close();
Log.d(TAG, "result = " + resultstring);
return new JSONObject(resultstring);
}