yy1.5HttpUtils文件下载

 1 public class MainActivity extends Activity {
 2 
 3     public static final String PATH = "https://www.baidu.com/";
 4     public static final String BAIKEPATH = "http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=news.getListByType";
 5     public static final String TOUTIAO = "http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=news.getHeadlines";
 6 
 7     public static final String APK = "http://121.15.220.153/sqdd.myapp.com/myapp/qqteam/AndroidQQ/mobileqq_android.apk?mkey=55235edec2a2372f&f=a10e&p=.apk";
 8     public static final String SMALL_APK = "http://fast.yingyonghui.com/1af72632d0b079cc77b7a54527403870/55237520/apk/877739/cn.com.jbit.assistant.1365571789396.apk";
 9 
10     private TextView mTv;
11     private ProgressBar mProgressBar;
12 
13     private HttpUtils mHttpUtils;
14 
15     private HttpHandler<File> mHttpHandler;
16 
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         mTv = (TextView) findViewById(R.id.tv);
23         mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);
24 
25         mProgressBar.setMax(100);
26         mProgressBar.setProgress(0);
27 
28         mHttpUtils = new HttpUtils();
29 
30         // 设置当前HttpUtils实例缓存超时的时间,以毫秒为单位
31         mHttpUtils.configCurrentHttpCacheExpiry(1);
32     }
33 
34     public void reStart(View view) {
35         File file = new File(Environment.getExternalStorageDirectory(), "keyiguanli.apk");
36 
37         // 文件的下载
38         mHttpHandler = mHttpUtils.download(APK, file.getAbsolutePath(), mDownloadCallBack);
39     }
40 
41     public void resume(View view) {
42         File file = new File(Environment.getExternalStorageDirectory(), "keyiguanli.apk");
43 
44         // mHttpHandler = mHttpUtils.download(SMALL_APK, file.getAbsolutePath(),
45         // true, mDownloadCallBack);
46         mHttpHandler = mHttpUtils.download(APK, file.getAbsolutePath(), true, false, mDownloadCallBack);
47     }
48 
49     public void pause(View view) {
50         mHttpHandler.cancel();
51     }
52 
53     private RequestCallBack<File> mDownloadCallBack = new RequestCallBack<File>() {
54 
55         private long mLastLoad;
56 
57         @Override
58         public void onSuccess(ResponseInfo<File> arg0) {
59             File file = arg0.result;
60 
61             // 通过Intent安装apk
62             Intent intent = new Intent(Intent.ACTION_VIEW);
63             intent.setDataAndType(Uri.parse("file://" + file.getAbsolutePath()), "application/vnd.android.package-archive");
64             startActivity(intent);
65 
66             Log.i("bm", file.getAbsolutePath() + "   " + file.exists());
67         }
68 
69         @Override
70         public void onLoading(long total, long current, boolean isUploading) {
71             int percent = (int) (100 * current / total);
72             mProgressBar.setProgress(percent);
73 
74             if (mLastLoad == 0) {
75                 mLastLoad = current;
76             }
77 
78             long speed = current - mLastLoad;
79 
80             mLastLoad = current;
81 
82             mTv.setText(speed / 1024 + " kb/s");
83         }
84 
85         public void onStart() {
86             mLastLoad = 0;
87         };
88 
89         @Override
90         public void onFailure(HttpException arg0, String arg1) {
91             Log.i("bm", arg1);
92         }
93     };
94 }

 

 1 public abstract class RequestCallBackEx<T> extends RequestCallBack<String> {
 2 
 3     private Class<T> mClass;
 4 
 5     public RequestCallBackEx(Class<T> cls) {
 6         mClass = cls;
 7     }
 8 
 9     @Override
10     public void onSuccess(ResponseInfo<String> arg0) {
11         try {
12             String data = arg0.result;
13 
14             if (data != null) {
15                 T obj = JSON.parseObject(data, mClass);
16                 onSuccess(obj);
17             }
18         } catch (Exception e) {
19             onFailure(new HttpException(e), e.toString());
20         }
21     }
22 
23     public abstract void onSuccess(T obj);
24 }

 

posted on 2015-04-07 19:36  CodeUtils  阅读(561)  评论(0编辑  收藏  举报

导航