StringBuilder无法追加字符串

时间:2014-03-28 12:00:20

标签: java android httprequest stringbuilder

我使用DefaultHttpClient从URL发出GET请求。 请参阅下面的代码。

你可以看到,如果用Log.i("result", l)记录结果,当循环结束时,我可以看到响应的完整数据, 但如果我使用htmlResult = sb.toString();我无法获取或查看所有数据,可能只有一半的响应。

这里有什么问题?感谢您的评论。

编辑:我发布了我的代码,每个人都可以帮助我:

public class DetailParser {
    HTTPAsyncRequest _asyncRequest;
    CompletionHandler _completion;
    private String _currentURL;
    private static DetailParser _instance;
    public GadgetItem _currentItem;
    static InputStream _inputStream = null;

    public static DetailParser getInstance()
    {
        if(_instance == null)
            _instance = new DetailParser();
        return _instance;
    }
    public class HTTPAsyncRequest extends AsyncTask<String, Integer, Response>
    {

        @Override
        protected Response doInBackground(String... params) {
            _currentURL = params[0];
            String result = getData(params[0]);
            Response response = new Response(result, params[0]);
            return response;
        }
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }
        @Override
        protected void onPostExecute(Response response) {
            if(response == null)
                return;
            if(!_currentURL.equals(response.urlRequest))
            {
                super.onPostExecute(response);
            }
            else
            {
                // TODO Auto-generated method stub
                parseGadgetItem(response.xmlResult);
                if(_completion != null)
                    _completion.getDescriptionDone(_currentItem);
                super.onPostExecute(response);
            }
        }
        public String getData(String url)
        {
private String _resultHTML = null;
            StringBuilder builder = new StringBuilder();
            BufferedReader reader = null;
            try {
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpPost = new HttpGet(url);    
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                _inputStream = httpEntity.getContent();
                reader = new BufferedReader(new InputStreamReader(_inputStream, "UTF-8"), 8);
                    StringBuilder sb = new StringBuilder();
                    String l = "";
                    String nl = System.getProperty("line.separator");
                    while ((l = reader.readLine()) != null) {
                        sb.append(l + nl);
//                      Log.i("result", l);
//                      htmlResult += l + nl;
                    }

                    _resultHTML = sb.toString();
//                  Log.i("Result", _resultHTML);
//              htmlResult = EntityUtils.toString(httpEntity);

            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    Log.e("GetMethodEx", e.getMessage());
                }
            }
            return _resultHTML;
        }
    }
    // constructor
    public DetailParser() {

    }

    private void abortRequest()
    {
        if(_asyncRequest != null)
            _asyncRequest.cancel(true);
    }

    public void getGadgetDescription(GadgetItem item, final CompletionHandler completion)
    {
        _currentItem = item;
        _completion = completion;
        abortRequest();
        _asyncRequest = (HTTPAsyncRequest) new HTTPAsyncRequest().execute(item.gadget_link,null,null);
    }

     public void parseGadgetItem(String html) {
// HERE : data is missing
            if (xml == null)
                return;
            try {
                Document doc = Jsoup.parse(html);
                Elements e = doc.getElementsByClass("thecontent entry-content");

// 
            } catch (Exception e) {
            }
        }
}

1 个答案:

答案 0 :(得分:0)

试试这个:

全球声明

static InputStream re = null;
private String result = null;

现在在您的课程中使用此代码

DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);

HttpResponse response1;

try {
    response1 = client.execute(httpGet);
    Log.e("Refresh is: ", response1.toString());
    HttpEntity entity = response1.getEntity();

    re=entity.getContent();
    BufferedReader reader = new BufferedReader(
        new InputStreamReader(re, "UTF-8"), 8);

    StringBuilder sb = new StringBuilder();

    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    String result= sb.toString();
    System.out.println(result);


} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
     e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}