Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid httpclient file upload data corruption and timeout issues
    primarykey
    data
    text
    <p>i'm having issues with uploading images in android.</p> <p>i'm using apache httpmime 4.1 lib the code is like this:</p> <pre><code>MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("image", new FileBody(new File(AndorraApplication.getPhotosPath() + "/" + entity.getFileName()), "image/jpeg")); resp = NetworkUtils.sendHttpRequestMultipart(EXPORT_PHOTOS_URI, reqEntity); </code></pre> <p>NetworkUtils class:</p> <pre><code>public class NetworkUtils { public static final int REGISTRATION_TIMEOUT = 3 * 1000; public static final int WAIT_TIMEOUT = 5 * 1000; public static HttpResponse sendHttpRequestMultipart(String uri, MultipartEntity entity) { HttpClient mHttpClient = new DefaultHttpClient(); final HttpParams params = mHttpClient.getParams(); HttpConnectionParams.setConnectionTimeout(params, REGISTRATION_TIMEOUT); HttpConnectionParams.setSoTimeout(params, WAIT_TIMEOUT); ConnManagerParams.setTimeout(params, WAIT_TIMEOUT); HttpPost post = new HttpPost(uri); post.addHeader(entity.getContentType()); post.setEntity(entity); HttpResponse resp = mHttpClient.execute(post); } } </code></pre> <p>sometimes everything works fine, but sometimes (especially on a slow connection) the image is uploaded very corrupted. the example is here: <a href="http://pixelbirthcloud.com/574_orig.jpg" rel="noreferrer">http://pixelbirthcloud.com/574_orig.jpg</a></p> <p>it doesn't throw any exceptions. the lenght of the uploaded file is same as the original one.. tried to change mime type to application/octet-stream or remove it at all. tries playing with timeouts. still same result. end users upload corrupted images almost all the time (although i managed to get bronem images just 2 times only).. the image size was 2.5 megs at first, but then i reduced it to 500-700 kb. didn't fix the problem though. </p> <p>haven't tried to change the apache's library.. maybe it is the problem.. but as far as i read the net, nobody was experiencing this with httpmime library.</p> <p>what can it be? i'm completely lost now :(</p> <p>the other issue is that timeouts sometimes not working.</p> <p>like when it comes to this line: HttpResponse resp = mHttpClient.execute(post); and i disable 3g connection it just waits for like 17-20 minutes instead of 3 or 5 seconds.. and only then throws exception. tried different methods. like this:</p> <pre><code> HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); HttpProtocolParams.setUseExpectContinue(params, false); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); ConnManagerParams.setMaxTotalConnections(params, 5); ConnManagerParams.setTimeout(params, 30000); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http",PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https",PlainSocketFactory.getSocketFactory(), 80)); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); HttpClient httpclient = new DefaultHttpClient(manager, params); </code></pre> <p>but still doesn't work :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload