HttpUrlConnection、HttpClient下载文件实例


下面是HttpUrlClient和HttpClient的下载实例:

public class AntherActivity extends Activity {
private ProgressDialog pd;
private long T,t;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void send_request(View v)
{
AsyncTask<String, Integer, Void> asyncTask  = new AsyncTask<String, Integer, Void>() {

@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pd = new ProgressDialog(AntherActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(100);
pd.setTitle("提示:");
pd.setMessage("正在下载!!!");
pd.show();
}
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub

// //解析这是使用HttpUrlClient下载
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);
try {
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setRequestMethod("GET");
connection.connect();
T= connection.getContentLength();
InputStream in = connection.getInputStream();
FileOutputStream fos = new  FileOutputStream("/mnt/sdcard/baidu/example2.mp3");
int count = 0;
byte [] b= new byte[1024];
while((count = in.read(b))!=-1)
{
fos.write(b, 0, count);
fos.flush();
t = count+t;
publishProgress((int)(t*100/T));
}
in.close();
fos.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}



} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/*这是使用 HttpClient下载

 HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
try {
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
T = entity.getContentLength();
InputStream  in = entity.getContent();
FileOutputStream fos = new FileOutputStream("/mnt/sdcard/baidu/example.mp3");
byte[] b= new byte[1024];
int count = 0;
while((count = in.read(b))!=-1)
{
fos.write(b, 0, count);
fos.flush();
t = t+count;
publishProgress((int)(100*t/T));
}
in.close();
fos.close();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
*/
return null;

}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
pd.setProgress(values[0]);
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pd.dismiss();
Toast.makeText(AntherActivity.this, "done", Toast.LENGTH_LONG).show();
}
};

//指定url;并开启asynctask
URL url = null;
try {
url = new URL("http://download.kugou.com/download/kugou_android");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
asyncTask.execute(url.toString());//开启asynctask

// url = new URL("http://download.kugou.com/download/kugou_android");
}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值