2

我正在使用 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上是否存在阻止从应用程序中快速上传文件的错误?

4

1 回答 1

3

Android 团队建议使用http://android-developers.blogspot.com/2011/09/androids-http-clients.htmlHttpUrlConnection代替HttpClient

于 2012-09-27T11:56:18.370 回答