【问题标题】:"org.apache.http.client.NonRepeatableRequestException" when uploading a picture from Android从 Android 上传图片时出现“org.apache.http.client.NonRepeatableRequestException”
【发布时间】:2012-09-15 04:23:55
【问题描述】:

我正在尝试从 Android 向 Rails 服务器发送图片。但是当我发送时,我收到以下错误。

09-19 23:22:11.810: W/System.err(2704): org.apache.http.client.ClientProtocolException                                                                                        
09-19 23:22:12.000: W/System.err(2704):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
09-19 23:22:12.020: W/System.err(2704):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
09-19 23:22:12.020: W/System.err(2704):     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:73)
09-19 23:22:12.050: W/System.err(2704):     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:92)
09-19 23:22:12.050: W/System.err(2704):     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
09-19 23:22:12.050: W/System.err(2704):     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:444)
09-19 23:22:12.050: W/System.err(2704):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
09-19 23:22:12.050: W/System.err(2704):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-19 23:22:12.050: W/System.err(2704):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-19 23:22:12.060: W/System.err(2704):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-19 23:22:12.070: W/System.err(2704):     at java.lang.Thread.run(Thread.java:1019)
09-19 23:22:12.250: W/System.err(2704): Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
09-19 23:22:12.320: W/System.err(2704):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:413)
09-19 23:22:12.360: W/System.err(2704):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
09-19 23:22:12.370: W/System.err(2704):     ... 10 more

奇怪的是,我每次上传图片时都不会收到此错误,只是有时在执行相同的过程时。这是我的代码。我正在使用“AsyncHttpClient”(http://loopj.com/android-async-http/)进行 http 连接。

public class AsynchConnector{
    static AsyncHttpClient client = new AsyncHttpClient();
    private static final String BASE_URL = Environment.SERVER_URL;

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler){
        Log.w("web", "sending POST requset to " + getAbsoluteUrl(url));
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }
}


public static void postPicture(String serverAlbumId, String serverUserId, String filePath){
    RequestParams params = new RequestParams();
    params.put("user_id", CameraApp.sp.getString("server_user_id", ""));
    params.put("picture[album_id]", serverAlbumId);
    params.put("picture[user_id]", serverUserId);

    try{
        params.put("picture[image]", StorageAccessor.getPictureAsFile(filePath));
    }catch(FileNotFoundException e){
        Log.e("---", "no image file found ");
    }

    Log.i("----------", "starting to post picture");
    AsynchConnector.post(PICTURE_PATH, params, new AsyncHttpResponseHandler(){
        public void onSuccess(String response) {
            Log.i("POST_PICTURE_RESOPNSE", response);
        }
    });
}

【问题讨论】:

  • 你有没有机会解决这个问题?我遇到了完全相同的问题,我的代码与您的非常相似。
  • 我认为这是由于在 AsynchHttpClient 中使用了 OutputStream 编写器。这篇文章可能有助于了解正在发生的事情。 old.nabble.com/… 我通过在 ResponseHandler 中实现 onFailure(Throwable error) 方法解决了这个问题,该方法再次执行相同的操作。 (在我的例子中,调用 postPicture 方法。)
  • 任何运气找到解决方案?

标签: android apache http file-upload


【解决方案1】:

这是由于在 AsynchHttpClient 中使用了 OutputStreamWriter 类。这篇文章可能有助于了解正在发生的事情。

http://httpcomponents.10934.n7.nabble.com/Http-Multi-part-exception-when-using-InputStreamBody-td12820.html

我通过在 ResponseHandler 中实现 onFailure(Throwable error) 方法解决了这个问题,该方法再次执行相同的操作。 (在我的例子中,调用 postPicture 方法。)

【讨论】:

  • 我尝试了这个解决方案,但现在我通过 onFailure 方法得到了一个永无止境的循环。我实施您的解决方案有误吗?
猜你喜欢
  • 1970-01-01
  • 2018-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 2017-11-28
  • 1970-01-01
相关资源
最近更新 更多