在我的应用程序中,我从公共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();
}
}
}
}
}
你从哪个网站获取数据。 THey可能有一个API,可以用来显着加快此过程。 – 2012-07-24 15:10:14