【问题标题】:Slow HttpPost request on AndroidAndroid 上的慢速 HttpPost 请求
【发布时间】:2012-10-15 06:33:00
【问题描述】:

我正在制作一个应用程序,其中一部分涉及从我的服务器以 JSON 格式下载数据。我正在使用 HTTP Post 来实现相同的目的。我正在下载的 JSON 文件大小约为 2-3 Kb。我在不同的线程上执行所有这些操作。

但是,我的应用程序的性能非常难以预测。我尝试调试它,发现有时我的应用程序卡在我正在创建 HttpPost 对象的行,有时执行该行几乎不需要 1-2 秒。可能是什么问题?可能是因为我的应用程序占用了大量内存吗?还有一件事,我的应用程序在我强制关闭后第二次运行良好。谢谢 !

public String getJSONString(String url) {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {sb.append(line + "n");}
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    return json;

}

}

【问题讨论】:

  • 告诉我,你在哪里测试你的应用程序?是在模拟器上还是在设备本身上?
  • 这一切都在与主 UI 线程不同的线程中运行吗?我希望如此,因为否则甚至不应该在所有版本的 Android 上工作。如何也发布线程创建,因为这可能有助于确定问题。
  • 我已经在设备和模拟器上测试过了。我在一个单独的线程上做它!

标签: android json http-post


【解决方案1】:

你可以试试这个代码..

                    // Create a new HttpClient and Post Header
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost(your URL) 

        // Sending details in Json Format
        JSONObject holder = new JSONObject();
        try { 
                     ------------
                   --------------

                } catch (JSONException e1) {
            e1.printStackTrace();
        }

    StringEntity se = new StringEntity(holder.toString());
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
    httppost.setEntity(se);
    response = httpclient.execute(httppost);



            // Sends data to server

        StringEntity se = new StringEntity(holder.toString());

        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));

        httppost.setEntity(se);

        response = httpclient.execute(httppost);

        resp = response.toString();

        String responseServer = EntityUtils.toString(response.getEntity());

【讨论】:

    【解决方案2】:

    我看到这篇文章时遇到了和你一样的问题

    HTTP Post requests using HttpClient take 2 seconds, why?

    你能确认它是否有效吗?

    HttpParams params = new BasicHttpParams();
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    HttpClient httpclient = new DefaultHttpClient(params);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 2011-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多