1)从指定的URL获取对应的流
private InputStream openHttpConnection(String urlString) throws IOException{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if(!(conn instanceof HttpURLConnection)){
throw new IOException("It is not an HTTP connection");
}
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
Log.v("Networking",ex.getLocalizedMessage());
throw new IOException("Error connecting");
}
return in;
}
private Bitmap downloadImage(String url){
Bitmap bitmap = null;
InputStream in = null;
try {
in = openHttpConnection(url);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.v("NetworkingActivity", e.getLocalizedMessage());
}
return bitmap;
}
private String downloadText(String url){
int BUFFER_SIZE = 2000;
InputStream is = null;
try {
is = openHttpConnection(url);
} catch (IOException e) {
// TODO Auto-generated catch block @camnpr
e.printStackTrace();
return "";
}
InputStreamReader isr = new InputStreamReader(is);
int charRead;
String str="";
char[] inputBuffer = new char[BUFFER_SIZE];
try {
while((charRead=isr.read(inputBuffer))>0){
String readString = String.copyValueOf(inputBuffer, 0, charRead);
str += readString;
inputBuffer = new char[BUFFER_SIZE];
}
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block @郑州网建
e.printStackTrace();
return "";
}
return str;
}
private class DownloadImageTask extends AsyncTask<String, Bitmap, Long>{
long imagesCount = 0;
ProgressBar progressBar;
public DownloadImageTask(ProgressBar pBar){
this.progressBar = pBar;
}
@Override
protected Long doInBackground(String... urls) {
// TODO Auto-generated method stub
for(int i = 0; i < urls.length;i++){
Bitmap imageDownloaded = downloadImage(urls[i]);
if(imageDownloaded!=null){
imagesCount ++;
publishProgress(imageDownloaded);
try {
Thread.sleep(300);
} catch (InterruptedException e) {
// TODO Auto-generated catch block@camnpr
e.printStackTrace();
}
}
}
return imagesCount;
}
//display the image downloaded
@Override
protected void onProgressUpdate(Bitmap... bitmaps) {
// TODO Auto-generated method stub
ivImg.setImageBitmap(bitmaps[0]);
progressBar.setProgress((int) imagesCount*10);
}
//when all the images have been downloaded
@Override
protected void onPostExecute(Long imageDownloaded) {
// TODO Auto-generated method stub
String str = "下载完成!一共下载了"+imagesCount +"张图片";
Toast.makeText(getBaseContext(), str, Toast.LENGTH_SHORT).show();
}
}
private class DownloadTextTask extends AsyncTask<String, Void, String>{
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub@ 郑州网建
Toast.makeText(getBaseContext(), result, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... urls) {
// TODO Auto-generated method stub
return downloadText(urls[0]);
}
}
//图片下载URLs
private String[] mUrl =
{
"upload/2014/10/201410291617535367.png",
"upload/2014/10/201410291617538135.png",
"upload/2014/10/201410291617546704.png",
"upload/2014/10/201410291617548700.png",
"upload/2014/10/201410291617554810.png",
"upload/2014/10/201410291617553457.png",
"upload/2014/10/201410291617567648.png",
"upload/2014/10/201410291617568687.png",
"upload/2014/10/201410291617570376.png",
"upload/2014/10/201410291617570767.png",
"upload/2014/10/201410291617585856.png",
"upload/2014/10/201410291617585716.png",
"upload/2014/10/201410291617595542.png",
"upload/2014/10/201410291617593754.png"
};
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(mUrl.length*10);
progressBar.setVisibility(View.VISIBLE);
//异步下载图片任务
DownloadImageTask task = new DownloadImageTask(progressBar);
task.execute(mUrl);
//文本文件URL @郑州网建
String strUrl = "http://www.sogou.com/docs/about.htm";
//异步下载文本文件内容任务
new DownloadTextTask().execute(strUrl);