arathunku
arathunku

Reputation: 1159

Source Code html doesn't download completly

I try to get HTML content, everything works find except 1 thing. It doesn't download whole code and skip the content which I want to extract(urls to images, names) and I have just blank classes 'obrazek'.

Here is the code i use to get source code:

     String SourceCode(String adres) throws IllegalStateException, IOException
{

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpGet httpGet = new HttpGet(adres);
    HttpResponse response = null;
    try {
         response = httpClient.execute(httpGet, localContext);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(response.getEntity().getContent()
                    )
            );
    String result = "";
    while(reader.readLine() != null)
    {
        result += reader.readLine();
    }
    reader.close();
    return result;

Thank you for help:)

Upvotes: 0

Views: 84

Answers (1)

MByD
MByD

Reputation: 137332

You skip one line each time. should be

StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
    result.append(line);
}
reader.close();
return result.toString();

BTW - I used StringBuilder to avoid creation of new String object each iteration - very recommended.

Upvotes: 3

Related Questions