下面是完整的方法。它以字符串形式返回给定 URL 的 HTML 代码。经过某些测试后,我得出的结论是 try 块被跳过了。但我不知道为什么。
public static String getHtml(String url)
{
StringBuilder result = new StringBuilder();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
char c = '"'; //The Java complains about this type of char So I removed it from the source.
while ((line = reader.readLine()) != null){
for(int i = line.length()-1; i >0; --i){
if (line.charAt(i) != c){
result.append(line);
}
}
}
return result.toString();
} catch(Exception e){
result.append("<p><img src=http://desperateshadows.files.wordpress.com/2013/02/img_0721.jpg?w=730 class=size-full alt=Green green not evergreen /></p><p>Little flower at a glance</p>"); //A test Source
return result.toString();
}
}