IllegalStateException:已消耗内容

时间:2011-05-06 04:47:10

标签: 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);
    }

在log cat中出现错误 IllegalStateException:内容已消耗。我无法找到的问题是什么。请帮我纠正此错误。

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

1 个答案:

答案 0 :(得分:6)

你要两次调用getContent():

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

使用in创建阅读器。