user1197941
user1197941

Reputation: 179

httpclient.Execute very slow on Android

I am using httpclient.execute(httppost) and its taking about 10 seconds to send a tiny file to my webserver. When I upload the same file using the web browser on the phone it takes less than 1 second. Here is my code

   String urlString = "http://xxxxx/upload.php";

        HttpParams params = new BasicHttpParams();
        params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        DefaultHttpClient httpclient = new DefaultHttpClient(params);
        HttpPost httppost = new HttpPost(urlString);

        File file = new File(pic);

        Log.d(TAG, "UPLOAD: setting up multipart entity");

        MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        Log.d(TAG, "UPLOAD: file length = " + file.length());
        Log.d(TAG, "UPLOAD: file exist = " + file.exists());

        mpEntity.addPart("uploadedfile", new FileBody(file, "application/octet"));
     // mpEntity.addPart("id", new StringBody("1"));

        httppost.setEntity(mpEntity);
        Log.d(TAG, "UPLOAD: executing request: " + httppost.getRequestLine());
        Log.d(TAG, "UPLOAD: request: " + httppost.getEntity().getContentType().toString());


        HttpResponse response;
        try {
            Log.d(TAG, "UPLOAD: about to execute");
            response = httpclient.execute(httppost);
            Log.d(TAG, "UPLOAD: executed");
            HttpEntity resEntity = response.getEntity();
            Log.d(TAG, "UPLOAD: respose code: " + response.getStatusLine().toString());
            if (resEntity != null) {
                Log.d(TAG, "UPLOAD: " + EntityUtils.toString(resEntity));
            }
            if (resEntity != null) {
                resEntity.consumeContent();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

Is there a bug on Android preventing the uploading of files quickly from within an App?

Upvotes: 2

Views: 4222

Answers (1)

vasart
vasart

Reputation: 6702

Android Team recommends using HttpUrlConnection instead of HttpClient http://android-developers.blogspot.com/2011/09/androids-http-clients.html

Upvotes: 3

Related Questions