0

我正在调用网络服务在地图上绘制点。我在回复中找到了 750 条记录。当页面第二次加载时,它向我显示了这个错误。

致命异常: org.apache.http.util.CharArrayBuffer.toString(CharArrayBuffer.java:261) 的 java.lang.String.(String.java:513) 的 org.apache.http 的 Thread-26 java.lang.OutOfMemoryError .util.EntityUtils.toString(EntityUtils.java:141) 在 org.apache.http.util.EntityUtils.toString(EntityUtils.java:146) 在 org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java :76) 在 org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657) 上的 org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)。 http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616) at com.roundmenu.Nearme.run(Nearme.java: 583)在java.lang。线程.run(线程.java:1019)

@Override
public void run() {
    try {
        HttpGet post = null;

        Log.e("", "Share.isfromfilter =  " + Share.isfromfilter);
        if (Share.isfromfilter) {

            Log.e("", "Teset 1");

            Log.e("", "Share.CuisineValue =  " + Share.CuisineValue);

            if (Share.CuisineValue.equalsIgnoreCase("")) {
                Log.e("", "Teset 2");
                post = new HttpGet("my url here");
            } else {
                Log.e("", "Teset 3");
                post = new HttpGet("my url here");
            }

        } else {
            Log.e("", "Teset 4");
            post = new HttpGet("my url here");

        }
        post.addHeader("apikey", "880639B3638597349AC1BE6F70FC5682");
        ResponseHandler<String> responseHandler = new BasicResponseHandler();

        response = client.execute(post, responseHandler);
        System.out.println("----------------------------------------");
        System.out.println(response);

        handler.sendEmptyMessage(0);
    } catch (Exception e) {
        e.printStackTrace();
        handler.sendEmptyMessage(1);
    }
}

在执行此代码期间,我也收到了 VM Out of Budget 错误。

4

1 回答 1

0

Android Dalvik VM 有堆限制。你能告诉 Web 服务返回多少字节吗?如果数据量很大,建议增量读取数据,而不是全部传输到一个大的字符串缓冲区中。

请参考http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpClient.html

HttpResponse response = httpclient.execute(post);
HttpEntity entity = response.getEntity();

if (entity != null) {
  InputStream instream = entity.getContent();
  try {
    BufferedReader reader = new BufferedReader(
             new InputStreamReader(instream));
    String data = null;
    while ((data = reader.readLine()) != null) {
      ...
    }
  }
}
于 2012-05-22T08:03:38.533 回答