httpclient.Execute在Android上非常慢

时间:2012-09-27 11:50:40

标签: android

我正在使用httpclient.execute(httppost),它花了大约10秒钟将一个小文件发送到我的网络服务器。当我使用手机上的网络浏览器上传相同的文件时,花费的时间不到1秒。这是我的代码

   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();
        }

Android上是否存在阻止从应用内快速上传文件的错误?

1 个答案:

答案 0 :(得分:3)

Android小组建议使用HttpUrlConnection代替HttpClient http://android-developers.blogspot.com/2011/09/androids-http-clients.html