我尝试获取HTML内容,除了1件事以外,一切都有效。它不下载整个代码并跳过我想要提取的内容(网址到图片,名称),我只有空白类'obrazek'。
以下是我用来获取源代码的代码:
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;
感谢您的帮助:)
答案 0 :(得分:3)
每次跳过一行。应该是
StringBuilder result = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
result.append(line);
}
reader.close();
return result.toString();
BTW - 我使用StringBuilder来避免每次迭代都创建新的String对象 - 非常推荐。