从Android上传图片时出现“org.apache.http.client.NonRepeatableRequestException”(“org.apache.http.client.NonRep

编程入门 行业动态 更新时间:2024-10-25 20:22:53
从Android上传图片时出现“org.apache.http.client.NonRepeatableRequestException”(“org.apache.http.client.NonRepeatableRequestException” when uploading a picture from Android)

我正试图从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); } }); }

I'm trying to send a picture to rails server from Android. But when I send, I get the following error.

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

What's weird is I don't get this error every time uploading a picture, just sometimes when doing the same process. Here are my codes. I'm using "AsyncHttpClient" (http://loopj.com/android-async-http/) for http connection.

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中使用的OutputStreamWriter类。 这篇文章可能有助于了解正在发生的事情。

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

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

That was due the OutputStreamWriter class used in AsynchHttpClient. This post might help understanding what's happening.

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

And I solved this by implementing onFailure(Throwable error) method in ResponseHandler that does the same thing again. (in my case, invoking postPicture method.)

更多推荐

本文发布于:2023-07-28 23:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310130.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上传图片   apache   org   Android   http

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!