public class HttpDownloader {
public URL url = null;
/**
* 根据URL下载文件,前提是这个文件是文本的,函数的返回值就是文件当中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建一个HttpURLConnection对象
* 3.得到InputStream
* 4.从InputStream得到数据
*/
public String download(String urlStr){
BufferedReader buffer = null;
StringBuffer sb = new StringBuffer();
String line = null;
try {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((line=buffer.readLine())!=null){
sb.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(buffer!=null){
buffer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}}java android
public URL url = null;
/**
* 根据URL下载文件,前提是这个文件是文本的,函数的返回值就是文件当中的内容
* 1.创建一个URL对象
* 2.通过URL对象,创建一个HttpURLConnection对象
* 3.得到InputStream
* 4.从InputStream得到数据
*/
public String download(String urlStr){
BufferedReader buffer = null;
StringBuffer sb = new StringBuffer();
String line = null;
try {
url = new URL(urlStr);
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
while((line=buffer.readLine())!=null){
sb.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(buffer!=null){
buffer.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sb.toString();
}}java android
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货