2
我使用的是httpclient.execute(httppost),它花了大約10秒鐘將一個小文件發送到我的網絡服務器。當我使用手機上的網絡瀏覽器上傳相同的文件時,它不到1秒鐘。這裏是我的代碼httpclient.Execute在Android上執行速度很慢
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上的一個錯誤,防止在應用程序內快速上傳文件?
感謝這個,我累了HttpURLConnection的也和其非常緩慢。我在這裏問了一個問題http://stackoverflow.com/questions/12581289/opening-datainputstream-running-very-slow。我決定嘗試使用另一種方法,因爲似乎沒有解決方案? – user1197941
也許這有幫助嗎? http://stackoverflow.com/questions/1920623/sometimes-httpurlconnection-getinputstream-executes-too-slowly – vasart
謝謝,它仍然是一樣的。上傳似乎很快就會發生,但讀取響應需要永久。 – user1197941