解决方案 »
- 网页获取数据
- 搜索匹配问题 求解
- 用setImageResource方法动态设置ImageView的图片形成动画效果,可行吗?
- android 用什么技术可以将简单英语或者汉字,转化成语音读出来呢。
- 写Android录像程序碰到的几个问题,请高手解答
- 菜鸟问题,在AndroidManifest.xml中添加Activity不成功。
- 请教,如何在谷歌上面发布应用?
- sqlite 查询 遍历 datetime
- 微信公众平台开发中遇到的一个疑问,请大神们指教!!!
- Android Studio新建项目只生成了空文件夹
- 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);
}