首页 > 代码库 > 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 30 private Button button; 31 private ProgressDialog dialog; 32 private String path; 33 private Handler handler = new Handler(){ 34 @Override 35 public void handleMessage(Message msg) { 36 dialog.setProgress(msg.what); 37 super.handleMessage(msg); 38 } 39 }; 40 41 @Override 42 protected void onCreate(Bundle savedInstanceState) { 43 super.onCreate(savedInstanceState); 44 setContentView(R.layout.activity_main2); 45 button = (Button) findViewById(R.id.but_id); 46 button.setOnClickListener(this); 47 dialog = new ProgressDialog(this); 48 } 49 50 @Override 51 public void onClick(View view) { 52 switch (view.getId()){ 53 case R.id.but_id: 54 dialog.setTitle("下载呢"); 55 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 56 dialog.show(); 57 new Thread(new Runnable() { 58 @Override 59 public void run() { 60 getURL(); 61 } 62 }).start(); 63 break; 64 } 65 } 66 67 /** 68 * 启动应用安装。 69 */ 70 private void setupApk() { 71 Intent intent = new Intent(Intent.ACTION_VIEW); 72 //"file://"+文件路径。 73 Uri uri = Uri.parse("file://"+ path); 74 intent.setDataAndType(uri, "application/vnd.android.package-archive"); 75 startActivity(intent); 76 } 77 78 private void getURL(){ 79 try { 80 //构建URL地址 81 URL url = new URL("http://g.pc6.com/0942666043/apk/4001_ZMJ2016_04_20161028_rnikgd.apk"); 82 try { 83 //打开打开打开 84 HttpURLConnection hcont = (HttpURLConnection) url.openConnection(); 85 //建立实际链接 86 hcont.connect(); 87 //获取输入流内容 88 InputStream is = hcont.getInputStream(); 89 //获取文件长度 90 int ddddd =hcont.getContentLength(); 91 //为进度条赋值 92 dialog.setMax(ddddd); 93 //手机存储地址 94 path = getExternalCacheDir().getPath()+"/wuxl.apk"; 95 //写入文件 96 OutputStream os = new FileOutputStream(path); 97 int length; 98 int lengtsh = 0; 99 byte [] bytes = new byte[1024]; 100 while ((length = is.read(bytes))!= -1){ 101 os.write(bytes,0,length); 102 //获取当前进度值 103 lengtsh+=length; 104 //把值传给handler 105 handler.sendEmptyMessage(lengtsh); 106 } 107 //关闭流 108 is.close(); 109 os.close(); 110 os.flush(); 111 dialog.dismiss(); 112 runOnUiThread(new Runnable() { 113 @Override 114 public void run() { 115 Toast.makeText(Main2Activity.this, "下载成功了", Toast.LENGTH_SHORT).show(); 116 } 117 }); 118 } catch (IOException e) { 119 e.printStackTrace(); 120 } 121 122 } catch (MalformedURLException e) { 123 e.printStackTrace(); 124 } 125 } 126 }
下面是布局文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 7 tools:context="com.wuxianedu.httpdemo.Main2Activity"> 8 9 <Button android:id="@+id/but_id" android:text="下载APP" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" /> 12 </RelativeLayout>
很简单,大家在使用的时候别忘记加网络权限哈
Android学习笔记--使用HttpURLConnection实现网络下载效果,附带进度条显示
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。