将图像发送到服务器时出现NonRepeatableRequestException

时间:2011-11-15 11:39:47

标签: android http

不时(特别是当我连接不好时)我获得了NonRepeatableRequestException 将图像发送到服务器。这是我的代码:

HttpParams params = new BasicHttpParams();

SchemeRegistry registry = new SchemeRegistry();
       registry.register(new Scheme("http",
PlainSocketFactory.getSocketFactory(), 80));

DefaultHttpClient httpclient = new DefaultHttpClient(new
ThreadSafeClientConnManager(params, registry), params);

HttpPost httpPost = new HttpPost(uri);
SimpleMultipartEntity multipartContent = new SimpleMultipartEntity();
multipartContent.addPart(propName, filename, inputStream,
contentType);
httpPost.setEntity(multipartContent);
httpclient.getParams().setParameter("http.protocol.expect-continue",
false);

java.util.logging.Logger.getLogger("httpclient.wire.header").setLevel(java.util.logging.Level.FINEST);
java.util.logging.Logger.getLogger("httpclient.wire.content").setLevel(java.util.logging.Level.FINEST);
HttpResponse response = httpclient.execute(httpPost);

我意识到InputStream是不可重复的类型,但不知道如何正确创建请求并避免此异常。 非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

发现我可以为我的httpclient添加自己的DefaultHttpRequestRetryHandler:

httpclient.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(3, false) {

    public boolean retryRequest(IOException exception, int executionCount,
            HttpContext context) {
             if (executionCount > this.getRetryCount()) {
                return false;
            }
             if (exception != null) {
                 return true;
             } else {
                 return super.retryRequest(exception, executionCount, context);
             }

    }
});

答案 1 :(得分:0)

我找到了引发此异常的原因之一。

根据this group,可能会引发此异常,因为服务器正在返回HTTP 401(未经授权)。