我使用HttpPost上传了一个多部分的数据块,并将其提供给一个HttpClient对象执行方法,如下所示:
HttpPost loginPost = new HttpPost(LOGIN_URL);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("_email", mEmailAddress));
params.add(new BasicNameValuePair("lpassword", mPassword));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "UTF-8");
loginPost.setEntity(entity);
HttpResponse resp = mHttpClient.execute(loginPost);
HttpPost post = new HttpPost(UPLOAD_URL);
FileBody bin = new FileBody(file);
MultipartEntity me = new MultipartEntity();
me.addPart("stuff", new StringBody(stuff));
me.addPart("file", bin);
post.setEntity(new RequestEntityEx(me, handler));
mHttpClient.execute(post);现在,登录并发布工作--很好,但上传速度慢得令人痛苦。我已经测试了我的互联网连接,它比它应该的速度慢得多(大约。上传速度为1Mb/s,上传一个3MB的文件大约需要5分钟(而不是30秒)。
有谁有什么想法吗?
发布于 2010-08-04 01:37:57
我发现HttpClient的速度比https上的普通方式慢9倍。我不知道为什么,谁都知道出了什么问题。
下面是我的代码
private static HttpClient httpClient = new DefaultHttpClient();
private static HttpPost httpPost = new HttpPost(RRD_URL);
public static String sendData(List<NameValuePair> data) {
StringBuffer buffer = new StringBuffer();
BufferedReader rd = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(data));
HttpResponse httpResponse = httpClient.execute(httpPost);
rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
String line;
while ((line = rd.readLine()) != null) {
buffer.append(line);
}
} catch (IOException e) {
e.printStackTrace(System.out);
} finally {
try {
if (rd != null) {
rd.close();
}
} catch (IOException e) {
e.printStackTrace(System.out);
}
}
return buffer.toString();
}https://stackoverflow.com/questions/2227025
复制相似问题