由于“无法使用不可重复的请求实体重试请求”,HttpPost失败

时间:2011-11-22 06:37:32

标签: android

我试图通过Android手机中的HttpClient将照片上传到服务器,但有时我会收到此错误,它不会一直发生。谁知道怎么解决呢?

我的代码如下:

HttpClient client =new DefaultHttpClient();
HttpPut httpPut = new HttpPut(uploadUrl);       
InputStream in =null;
HttpResponse response =null;
        try {
            in = new FileInputStream(filepath);
            BasicHttpEntity entity = new BasicHttpEntity();
            entity.setContent(in);
            entity.setContentLength(new File(basepath+path).length());

            httpPut.setEntity(entity);
            response = client.execute(httpPut);
            System.out.println(response.getStatusLine());


            consume(response.getEntity());


        } 

.......

错误信息:

11-22 10:32:50.370: W/System.err(15224): org.apache.http.client.ClientProtocolException
11-22 10:32:50.370: W/System.err(15224):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
11-22 10:32:50.370: W/System.err(15224):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-22 10:32:50.370: W/System.err(15224):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
11-22 10:32:50.370: W/System.err(15224):    at com.hp.nimbus.android.skydrive.util.SkyDriveUtils_Rest.doUploadPhoto(SkyDriveUtils_Rest.java:203)
11-22 10:32:50.380: W/System.err(15224):    at com.hp.nimbus.android.skydrive.util.SkyDriveUtils_Rest.uploadPhoto(SkyDriveUtils_Rest.java:250)
11-22 10:32:50.380: W/System.err(15224):    at com.hp.nimbus.android.skydrive.FolderMonitorService$1.onEvent(FolderMonitorService.java:36)
11-22 10:32:50.380: W/System.err(15224):    at android.os.FileObserver$ObserverThread.onEvent(FileObserver.java:125)
11-22 10:32:50.380: W/System.err(15224):    at android.os.FileObserver$ObserverThread.observe(Native Method)
11-22 10:32:50.380: W/System.err(15224):    at android.os.FileObserver$ObserverThread.run(FileObserver.java:88)
11-22 10:32:50.380: W/System.err(15224): Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
11-22 10:32:50.380: W/System.err(15224):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:419)
11-22 10:32:50.380: W/System.err(15224):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-22 10:32:50.390: W/System.err(15224):    ... 8 more

2 个答案:

答案 0 :(得分:6)

这意味着初始请求失败,因为它是一个流,所以它不能重复它。

如果您删除了FileInputStream而改为使用FileEntity,我相信它会重复。

您也不必设置长度。

答案 1 :(得分:2)

回答这个问题可能要迟一点,但对于登陆此页面的人来说,BasicHttpEntity例外的原因是HttpEntity不允许重复请求。您需要使用正确的FileEntity实现。

对于这个问题的情况,它将是@echo on SET userid=root SET passwd=Welcome1% for /f "delims=" %%i in ('type "ipaddress.txt" ') do ( pushd "C:\Program Files (x86)\PuTTY" plink.exe -pw %passwd% %userid%@%%i hostname popd ) ,正如Matthew Flaschen在另一个答案中提到的那样,但对于大多数其他情况,可以使用StringEntity来允许可重复的请求。