今天学习 了Apache 写了一个网络下载的效果,代码如下,代码中我已经写了很详细的注释了,相信大家都能看懂
1 http://m.blog.csdn.net/article/details?id=52927536 2 3 4 package com.wuxianedu.httpdemo; 5 6 import android.app.ProgressDialog; 7 import android.content.Intent; 8 import android.net.Uri; 9 import android.os.AsyncTask; 10 import android.support.v7.app.AppCompatActivity; 11 import android.os.Bundle; 12 import android.view.View; 13 import android.widget.Button; 14 import android.widget.Toast; 15 16 import org.apache.http.HttpResponse; 17 import org.apache.http.client.HttpClient; 18 import org.apache.http.client.methods.HttpGet; 19 import org.apache.http.impl.client.DefaultHttpClient; 20 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.OutputStream; 25 26 public class Main2Activity extends AppCompatActivity implements View.OnClickListener{ 27 28 private Button button; 29 private ProgressDialog dialog; 30 private String path; 31 private final int TA_C = 0;//成功 32 private final int TA_S = -1;//失败 33 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main2); 38 button = (Button) findViewById(R.id.but_id); 39 button.setOnClickListener(this); 40 } 41 42 @Override 43 public void onClick(View view) { 44 switch (view.getId()){ 45 case R.id.but_id: 46 new Myhui().execute("http://g.pc6.com/0942666043/apk/4001_ZMJ2016_04_20161028_rnikgd.apk"); 47 break; 48 } 49 } 50 51 class Myhui extends AsyncTask<String,Integer,Integer>{ 52 //异步任务执行前的操作 53 @Override 54 protected void onPreExecute() { 55 dialog = new ProgressDialog(Main2Activity.this); 56 dialog.setTitle("正在下载"); 57 dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 58 dialog.show(); 59 super.onPreExecute(); 60 } 61 //更新进度的方法 调用使用publishProgress 62 @Override 63 protected void onProgressUpdate(Integer... values) { 64 dialog.setProgress(values[0]); 65 super.onProgressUpdate(values); 66 } 67 //任务执行中方法 68 @Override 69 protected Integer doInBackground(String... strings) { 70 //构建URL地址链接 71 HttpGet httpGet = new HttpGet(strings[0]); 72 //建立客户端链接 73 HttpClient httpClient = new DefaultHttpClient(); 74 try { 75 //获取服务器响应的数据 76 HttpResponse response = httpClient.execute(httpGet); 77 //获取状态码 78 int code = response.getStatusLine().getStatusCode(); 79 switch (code){ 80 case 201: 81 case 200: 82 //获取文件最大值 83 int zuimax = (int) response.getEntity().getContentLength(); 84 //给进度条赋最大值 85 dialog.setMax(zuimax); 86 //获取文件的输入流 87 InputStream is = response.getEntity().getContent(); 88 //获取储存地址 89 path = getExternalCacheDir().getParent()+"/tanchishe.apk"; 90 //获取文件的输出流,传入写入文件的地址 91 OutputStream os = new FileOutputStream(path); 92 int length; 93 //进度条进度 94 int jindu = 0; 95 byte[] bytes = new byte[1024]; 96 while ((length = is.read(bytes))!= -1){ 97 os.write(bytes,0,length); 98 jindu+=length; 99 //启动赋值方法 100 publishProgress(jindu); 101 } 102 //关闭流 103 is.close(); 104 os.flush(); 105 os.close(); 106 return TA_C; 107 case 401: 108 break; 109 } 110 } catch (IOException e) { 111 e.printStackTrace(); 112 } 113 return TA_S; 114 } 115 116 //任务结束方法 117 @Override 118 protected void onPostExecute(Integer integer) { 119 //关闭进度条 120 dialog.dismiss(); 121 switch (integer){ 122 case TA_C: 123 Toast.makeText(Main2Activity.this, "下载成功了", Toast.LENGTH_SHORT).show(); 124 setupApk(); 125 break; 126 case TA_S: 127 Toast.makeText(Main2Activity.this, "下载失败了", Toast.LENGTH_SHORT).show(); 128 break; 129 } 130 super.onPostExecute(integer); 131 } 132 } 133 134 /** 135 * 启动应用安装。 136 */ 137 private void setupApk() { 138 Intent intent = new Intent(Intent.ACTION_VIEW); 139 //"file://"+文件路径。 140 Uri uri = Uri.parse("file://"+ path); 141 intent.setDataAndType(uri, "application/vnd.android.package-archive"); 142 startActivity(intent); 143 } 144 }
下面是布局文件
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>