我正在使用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秒)。
有人有什么想法吗?
答案 0 :(得分:0)
我发现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();
}