在我的应用程序中,我从公共 URL 发出请求,然后打开网页的源代码,最后从源代码中提取我想要的信息。我对整个过程没有任何问题。但是,加载我想要的信息需要很长时间。我还有其他有效的方法吗?
public class GetMethodEx {
public String getInternetData(String currentUrl) throws Exception{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI(currentUrl);
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while((l = in.readLine()) !=null){
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}finally{
if (in != null){
try{
in.close();
return data;
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}