开发者

AsyncTask limits buffers size?

I'm trying to get html content. Everything works in AsyncTask thread. This is my code:

开发者_开发知识库    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();           
}  

Problem is, that I get only part of response. The first debug shows, that while loop sees all content, but the second debug, which shows info from StringBuffer, shows only part of content. So, there are any limits for using StringBuffer in AsyncTask?

I tried to use EntitiyUtils, but got same problem:

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

Any solutions, tips?

Thanks in advance!


Try:

   @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;
}

EDIT

The problem was logcat. It truncated the logmessages (see comments)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜