AsyncTask限制缓冲区大小?

时间:2011-07-03 11:14:54

标签: android string buffer android-asynctask http-get

我正在尝试获取HTML内容。一切都在AsyncTask线程中工作。这是我的代码:

    protected String doInBackground(String... params) {         
        InputStream content = null;
        try {
            HttpGet httpGet = new HttpGet(params[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httpGet);                                        
            content = response.getEntity().getContent(); 
        } catch (Exception e) {

        }                       
        if (content == null) {
            return "Error";
        }
        BufferedReader in = new BufferedReader(new InputStreamReader(content));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        try {
            while (((line = in.readLine()) != null)) {
                Log.i("DEBUG", line);
                sb.append(line + NL);
            }
            Log.i("DEBUG2", sb.toString());
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();           
}  

问题是,我只得到了一部分回复。第一个调试显示,while循环查看所有内容,但第二个调试(显示来自StringBuffer的信息)仅显示部分内容。那么,在AsyncTask中使用StringBuffer有什么限制吗?

我尝试使用EntitiyUtils,但遇到同样的问题:

return new String(EntityUtils.toString(entity));

任何解决方案,提示?

提前致谢!

1 个答案:

答案 0 :(得分:0)

尝试:

   @Override
   protected String doInBackground(String... params) {

    HttpGet get = new HttpGet(params[0]);       
    HttpClient client = new DefaultHttpClient();
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        HttpResponse response = client.execute(get);
        return responseHandler.handleResponse(response);           
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }       
    return null;
}

修改

问题是logcat。它截断了logmessages(见注释)