如题所示:这个问题是在http请求后操作response产生的。

读取JSON

        /*** JSON返回常用String* * @param response* @return* @throws IllegalStateException* @throws IOException* @throws JSONException*/public static String getStringByJson(String field,HttpResponse response) throws IllegalStateException, IOException, JSONException{StringBuilder builder = new StringBuilder();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String s = null;while ((s = bufferedReader.readLine()) != null){builder.append(s);}JSONObject jsonDataInfo = new JSONObject(builder.toString());return jsonDataInfo.getString(field);}/*** JSON返回常用int* * @param response* @return* @throws IllegalStateException* @throws IOException* @throws JSONException*/public static int getIntByJson(String field,HttpResponse response) throws IllegalStateException, IOException, JSONException{StringBuilder builder = new StringBuilder();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));String s = null;while ((s = bufferedReader.readLine()) != null){builder.append(s);}JSONObject jsonDataInfo = new JSONObject(builder.toString());return jsonDataInfo.getInt(field);}/*** 获取钻井任务封装model对象集合* * @param response* @param clazz* @return* @throws IllegalStateException* @throws IOException* @throws JSONException  */@SuppressWarnings({ "unchecked", "rawtypes" })public static SchedulerModel getSchedulerModelByJson(String field,HttpResponse response, Class clazz) throws IllegalStateException, IOException, JSONException{String resultString=StringUtil.getStringByJson(field, response);ObjectMapper om = new ObjectMapper();SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");om.getSerializationConfig().setDateFormat(myDateFormat);return om.readValue(resultString, clazz);}

JSON转对象

 try{// 设置请求参数项request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));HttpClient client = HttpClientUtil.getInstance();// 执行请求返回相应HttpResponse response = client.execute(request);if (response.getStatusLine().getStatusCode() == 200){schedulerModel=StringUtil.getSchedulerModelByJson("model", response, SchedulerModel.class);if (schedulerModel!=null){Log.d("log", ">>>>执行 方法addToServer(Scheduler scheduler) ,id=" + schedulerModel.getId());}} else{Log.d("log", ">>>>connectionError");}} catch (Exception e){Log.d("log", ">>>>connectionError" + e.getMessage());// e.printStackTrace();Log.d("log", ">>>>执行 方法addToServer(Scheduler scheduler) ,出错。" + e.getMessage());}

异常分析

如果在调用

StringUtil.getSchedulerModelByJson("model", response, SchedulerModel.class);

之前或者之后调用,有使用到response,只要调用了一次

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

那么,都将会返回: Content has been consumed异常,因为content读取之后就被销毁了。

代码提升

如果要实现多次读取就只有将content内容保存在一个临时对象上,如StringBuilder

/*** 获取返回值内容* @param response* @return* @throws IOException*/public static StringBuilder getStringBuilder(HttpResponse response) throws IOException{StringBuilder builder = new StringBuilder();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(),HTTP.UTF_8));String s = null;while ((s = bufferedReader.readLine()) != null){builder.append(s);}return builder;}

以后获取JSON数据的时候就方便多了,代码如下:

 /*** JSON返回常用String* * @param response* @return* @throws IllegalStateException* @throws IOException* @throws JSONException*/public static String getStringByJson(String field,StringBuilder builder) throws IllegalStateException, IOException, JSONException{     JSONObject jsonDataInfo = new JSONObject(builder.toString());return jsonDataInfo.getString(field);}/*** JSON返回常用int* * @param response* @return* @throws IllegalStateException* @throws IOException* @throws JSONException*/public static int getIntByJson(String field,StringBuilder builder) throws IllegalStateException, IOException, JSONException{JSONObject jsonDataInfo = new JSONObject(builder.toString());return jsonDataInfo.getInt(field);}

Android:Content has been consumed相关推荐

  1. AndroidStudio中提示:android.content.res.Resources NotFoundException: String resource ID 0x

    场景 在Android Studio中对TextView调用setText时传入的是Int类型的值,就会提示: android.content.res.Resources NotFoundExcept ...

  2. android.content.Context.getResources()‘ on a null object reference

    super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); //开始运行时就把库先建好 //我在这边一开始 ...

  3. Android Content Provider Security

    0x00 科普 内容提供器用来存放和获取数据并使这些数据可以被所有的应用程序访问.它们是应用程序之间共享数据的唯一方法:不包括所有Android软件包都能访问的公共储存区域.Android为常见数据类 ...

  4. android.content.ActivityNotFoundException: No Activity found to handle Intent 的错误

    android.content.ActivityNotFoundException: No Activity found to handle Intent {  }的错误原因 是声明Intent的两个 ...

  5. XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager,

    android开发之多个provider的解决方法: 下面的报错针对android7.0及以上都是一个问题就是provider的authorities没有对上. 看下报错代码: java.lang.N ...

  6. Android之提示android.content.res.Resources$NotFoundException: Resource ID #0x7f08010a

    1 问题 在我的oppo机器上面运行一个页面没有奔溃,但是在红米安卓5.1版本运行页面奔溃,奔溃信息如下 /Timeline(11600): Timeline: Activity_launch_req ...

  7. Android报“android.content.res.Resources$NotFoundException: String resource ID #0x2”错误

    Android报"android.content.res.Resources$NotFoundException: String resource ID #0x2"错误 当调用se ...

  8. android Content provider 组件

    Content Provider 属于Android应用程序的组件之一,作为应用程序之间唯一的共享数据的途径,Content Provider 主要的功能就是存储并检索数据以及向其他应用程序提供访问数 ...

  9. android.content.res.Resources$NotFoundException: String resource ID XXXX

    错误:android.content.res.Resources$NotFoundException: String resource ID XXXX 原因:一般发生在参数 int resId 错误, ...

  10. Android Content Provider基础

    Android Content Provider基础 Content Providers Content providers管理对一个结构化的数据集合的访问.它们封装了数据,并且提供了保护数据安全性的 ...

最新文章

  1. 条件随机场(CRF) - 2 - 定义和形式
  2. 基于运动相关分析的实时多源异构传感器时空标定方法研究
  3. 第二天2017/03/29: 字符串操作
  4. php中cookie的用法
  5. rabbit MQ 的环境及命令使用(一)
  6. 远程桌面(3389)复制(拖动)文件
  7. [转]浅谈封装中的非技术细节 - 含减肥攻略
  8. arcgis裁剪多个shp文件_ArcGIS批量裁剪矢量数据
  9. 美食app的UI设计
  10. ih5长图如何滑动_iH5中级教程:微场景H5必备,横向滑动切换
  11. 软件测试:给你一个网站,你如何测试
  12. JVM垃圾清理机制详解 ✨ 每日积累
  13. 制作U盘启动来安装Linux系统的具体方法(图文)
  14. 究竟什么时候该看哪个后视镜?老司机用经验告诉你答案
  15. puzzle(1015)明灯谜局、马赛克
  16. AMD5600G LOL卡顿
  17. php影视网站主页不显示列表,七星修改米酷影视6.2.8完整版–PHP7.0及以上,修复页面不显示和不能播放等...
  18. 富士胶片 激光一体机 ApeosPort 3410SD安装配置心得
  19. 总结了120个数据指标与术语
  20. 要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9) 也不相同,每个字符串长度为10;

热门文章

  1. Vue项目实战:订单列表页面实现
  2. OpenStack部署(图文详解)
  3. 淘宝API接口调用:item_search_img - 按图搜索淘宝商品(拍立淘)
  4. 【绘图】python 配色表
  5. 移动电子商务:五个技术标准与Trustonic TEE解决方案【转】
  6. 外贸公司邮箱大全,选择哪个企业邮箱最好用?
  7. 企业邮箱哪个最好用?哪个企业邮箱更优惠?费用是多少?
  8. python监听鼠标键盘_python用pynput监听控制键盘鼠标
  9. 童星养成系统的文推荐_戏骨、童星、实力派,《隐秘的角落》全员数据图鉴
  10. 联想笔记本G50-70无线网卡问题