0

我正在使用 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 秒)。

有人有想法么?

4

1 回答 1

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();
}
于 2010-08-03T17:37:56.953 回答