Android uses open source project Xutils to implement multi-threaded download files

Source: Internet
Author: User

#1. Download the Utils Project

Https://github.com/wyouflf/xUtils


#2布局文件里实现UI


<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:orientation= "vertical"    tools:context= ". Mainactivity ">    <edittext        android:id=" @+id/tv_path "        android:layout_width=" Match_parent        " android:layout_height= "Wrap_content"        android:text= "Http://down.360safe.com/inst.exe"/>    <button        android:layout_margintop= "10dip"        android:layout_width= "match_parent"        android:onclick= "Download "        android:layout_height=" wrap_content "        android:text=" Download "         />    <textview         android: Id= "@+id/tv_info"          android:layout_width= "match_parent"          android:layout_margintop= "10dip"        android: layout_height= "Wrap_content"        android:text= "message"         />    </LinearLayout>

#3. Implementing code functionality in Mainactivity

Package Com.wzw.downloaddemo;import Java.io.file;import Com.lidroid.xutils.httputils;import Com.lidroid.xutils.exception.httpexception;import Com.lidroid.xutils.http.httphandler;import Com.lidroid.xutils.http.responseinfo;import Com.lidroid.xutils.http.callback.requestcallback;import Android.os.bundle;import Android.app.activity;import Android.view.menu;import Android.view.View;import Android.widget.edittext;import Android.widget.textview;public class Mainactivity extends Activity {private EditText Etpath;private TextView tvinfo; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.activity_main); etpath= (EditText) Findviewbyid (R.id.tv_path); tvInfo= (TextView) Findviewbyid (r.id.tv_info);} public void Download (View v) {String path =etpath.gettext (). toString (); Httputils http = new Httputils (); Http.download (Path, "/sdcard/360.exe", True, True, new Requestcallback<file> () {& Lt;span style= "White-space:pre" ></span>@Override public void OnStart () {tvinfo.settext ("Connecting ..."); } @Override public void onloading (long, Long, Boolean isuploading) {Tvinfo.settext (current +) /"+ total);      } @Override public void OnFailure (httpexception error, String msg) {tvinfo.settext (msg); } @Overridepublic void onsuccess (responseinfo<file> responseinfo) {//TODO auto-generated method Stubtvinfo.settext ("Downloaded:" + responseInfo.result.getPath ());}});}}


It is more convenient to use utils words than in traditional ways.


Android uses open source project Xutils to implement multi-threaded download files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.