Android学习笔记--使用HttpURLConnection实现网络下载效果,附带进度条显示

幼敏悟过人,读书辄成诵。这篇文章主要讲述Android学习笔记--使用HttpURLConnection实现网络下载效果,附带进度条显示相关的知识,希望能为你提供帮助。
下面我就直接上代码了,因为代码中我已经写了非常详细的注释

1 package com.wuxianedu.httpdemo; 2 3 import android.app.ProgressDialog; 4 import android.content.Intent; 5 import android.net.Uri; 6 import android.os.AsyncTask; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.support.v7.app.AppCompatActivity; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.Toast; 14 15 import org.apache.http.HttpResponse; 16 import org.apache.http.client.HttpClient; 17 import org.apache.http.client.methods.HttpGet; 18 import org.apache.http.impl.client.DefaultHttpClient; 19 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.OutputStream; 24 import java.net.HttpURLConnection; 25 import java.net.MalformedURLException; 26 import java.net.URL; 27 28 public class Main2Activity extends AppCompatActivity implements View.OnClickListener{ 29 30private Button button; 31private ProgressDialog dialog; 32private String path; 33private Handler handler = new Handler(){ 34@Override 35public void handleMessage(Message msg) { 36dialog.setProgress(msg.what); 37super.handleMessage(msg); 38} 39}; 40 41@Override 42protected void onCreate(Bundle savedInstanceState) { 43super.onCreate(savedInstanceState); 44setContentView(R.layout.activity_main2); 45button = (Button) findViewById(R.id.but_id); 46button.setOnClickListener(this); 47dialog = new ProgressDialog(this); 48} 49 50@Override 51public void onClick(View view) { 52switch (view.getId()){ 53case R.id.but_id: 54dialog.setTitle("下载呢"); 55dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 56dialog.show(); 57new Thread(new Runnable() { 58@Override 59public void run() { 60getURL(); 61} 62}).start(); 63break; 64} 65} 66 67/** 68* 启动应用安装。 69*/ 70private void setupApk() { 71Intent intent = new Intent(Intent.ACTION_VIEW); 72//"file://"+文件路径。 73Uri uri = Uri.parse("file://"+ path); 74intent.setDataAndType(uri, "application/vnd.android.package-archive"); 75startActivity(intent); 76} 77 78private void getURL(){ 79try { 80//构建URL地址 81URL url = new URL("http://g.pc6.com/0942666043/apk/4001_ZMJ2016_04_20161028_rnikgd.apk"); 82try { 83//打开打开打开 84HttpURLConnection hcont = (HttpURLConnection) url.openConnection(); 85//建立实际链接 86hcont.connect(); 87//获取输入流内容 88InputStream is = hcont.getInputStream(); 89//获取文件长度 90int ddddd =hcont.getContentLength(); 91//为进度条赋值 92dialog.setMax(ddddd); 93//手机存储地址 94path = getExternalCacheDir().getPath()+"/wuxl.apk"; 95//写入文件 96OutputStream os = new FileOutputStream(path); 97int length; 98int lengtsh = 0; 99byte [] bytes = new byte[1024]; 100while ((length = is.read(bytes))!= -1){ 101os.write(bytes,0,length); 102//获取当前进度值 103lengtsh+=length; 104//把值传给handler 105handler.sendEmptyMessage(lengtsh); 106} 107//关闭流 108is.close(); 109os.close(); 110os.flush(); 111dialog.dismiss(); 112runOnUiThread(new Runnable() { 113@Override 114public void run() { 115Toast.makeText(Main2Activity.this, "下载成功了", Toast.LENGTH_SHORT).show(); 116} 117}); 118} catch (IOException e) { 119e.printStackTrace(); 120} 121 122} catch (MalformedURLException e) { 123e.printStackTrace(); 124} 125} 126 }

下面是布局文件
1 < ?xml version="1.0" encoding="utf-8"?> 2 < RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3xmlns:tools="http://schemas.android.com/tools" 4android:layout_width="match_parent" 5android:layout_height="match_parent" 6 7tools:context="com.wuxianedu.httpdemo.Main2Activity"> 8 9< Button android:id="@+id/but_id" android:text="下载APP" 10android:layout_width="wrap_content" 11android:layout_height="wrap_content" /> 12 < /RelativeLayout>

【Android学习笔记--使用HttpURLConnection实现网络下载效果,附带进度条显示】很简单,大家在使用的时候别忘记加网络权限哈

    推荐阅读