您的位置:首页 > 理论基础 > 计算机网络

HttpUrlConnection、HttpClient下载文件实例

2016-09-19 11:22 323 查看
下面是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));<
4000
br />}
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");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: