android - 非法状态异常 : Content has been consumed

标签 android

class VideoUploadTask extends AsyncTask<Void, Void, String> {
    @Override
    protected String doInBackground(Void... unsued) {
        try {
            Log.i("VideoUploadTask", "enters");

            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(
                "http://192.168.5.10/ijoomer_development/index.php?option=com_ijoomer&plg_name=jomsocial&pview=album&ptask=upload_video&session_id="+ConstantData.session_id +"");
            MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            // bitmap.compress(CompressFormat.JPEG, 100, bos);
            byte[] data = bos.toByteArray();
            Log.i("categoryId in vedio upload2", ""+ConstantData.categoryId);
            entity.addPart("categoryId", new StringBody(ConstantData.categoryId.toString()));
            entity.addPart("video", new ByteArrayBody(data, "myvideo.mp4"));
            /*
             * entity.addPart("photoCaption", new
             * StringBody(caption.getText() .toString()));
             */
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost,localContext);
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));

            String sResponse = reader.readLine();

            String strResponse = convertStreamToString(in);
            System.out.println(strResponse);
            if (dialog.isShowing())
                dialog.dismiss();

            return strResponse;

        } catch (Exception e) {
            if (dialog.isShowing())
                dialog.dismiss();
            // Toast.makeText(getApplicationContext(),"Exception Image",Toast.LENGTH_LONG).show();
            Log.e(e.getClass().getName(), e.getMessage(), e);
            return null;
        }

        // (null);
    }

在日志cat中出现错误IllegalStateException:内容已被消耗。我找不到问题所在。请帮助我纠正此错误。

IllegalStateException:内容已被消耗问题已解决。但通过这种编码我无法上传视频。请给我一些建议。

最佳答案

您调用 getContent() 两次:

            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            response.getEntity().getContent(), "UTF-8"));

使用in创建阅读器。

关于android - 非法状态异常 : Content has been consumed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5906890/

相关文章:

android - 用 Int[] 填充 ImageAdapter

android - Android 项目中的环境变量

android - 如何使用 root 在 Android 4.2 及更高版本上切换飞行模式?

java - 在后台对 Eddystone Beacon 使用react

android - 如何将 SurfaceTexture 保存为位图

java - 我存储数据的方式可能存在问题..?

android - 如何在android中获取当前位置的经纬度

android - 反向地理编码 - 服务不可用 - 重启设备没有明确解决 - 批评我的手动反向地理编码

android - 如何在两个不同的端口接收数据短信

android - 无效的 APK 文件