XUTILS3 file Upload, download, get, Post requests

Source: Internet
Author: User
Tags throwable

@ContentView (r.layout.activity_xutils3_net) Public classXutils3netactivityextendsActivity {@ViewInject (r.id.tv_result)PrivateTextView TextView; @ViewInject (R.id.progressbar)PrivateProgressBar ProgressBar; @ViewInject (R.id.tv_title)PrivateTextView title; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);//Setcontentview (r.layout.activity_xutils3_net);X.view (). Inject ( This); Title.settext ("XUTILS3 Network Module"); } @Event (Value={r.id.btn_get_post,r.id.btn_downloadfile,r.id.btn_uploadfile})Private voidgetEvent (view view) {Switch(View.getid ()) { Caser.id.btn_get_post:getandpostnet ();//Toast.maketext (Xutils3netactivity.this, "Get and Post requests", Toast.length_short). Show ();                 Break;  Caser.id.btn_downloadfile:downloadfile ();//Toast.maketext (xutils3netactivity.this, "File Download", Toast.length_short). Show ();                 Break;  Caser.id.btn_uploadfile:uploadfile ();//Toast.maketext (xutils3netactivity.this, "File Upload", Toast.length_short). Show ();                 Break; }    }    /*** File Upload*/    Private voidUploadFile () {requestparams params=NewRequestparams ("Http://192.168.1.16:8080/FileUpload/FileUploadServlet"); //upload as a formParams.setmultipart (true); //set the path to the upload fileParams.addbodyparameter ("File",NewFile (Environment.getexternalstoragedirectory () + "/atguigu/480.mp4"),NULL, "Oppo.mp4"); X.http (). Post (params,NewCallback.progresscallback<file>() {            /*** Callback This method when the download succeeds, and return the downloaded path to it.@paramfile*/@Override Public voidonsuccess (file file) {LOG.E ("TAG", "onsuccess==" +file.tostring ()); Toast.maketext (xutils3netactivity. This, "onsuccess==" +file.tostring (), Toast.length_short). Show (); } @Override Public voidOnError (Throwable ex,Booleanisoncallback) {LOG.E ("TAG", "onerror==" +ex.getmessage ()); } @Override Public voidoncancelled (cancelledexception CeX) {LOG.E ("TAG", "oncancelled==" +cex.getmessage ()); } @Override Public voidonfinished () {LOG.E ("TAG", "onfinished=="); } @Override Public voidonwaiting () {LOG.E ("TAG", "onwaiting=="); } @Override Public voidonstarted () {LOG.E ("TAG", "onstarted=="); } @Override Public voidOnloading (LongTotalLongCurrentBooleanisdownloading) {Progressbar.setmax (int) total); Progressbar.setprogress ((int) (current); LOG.E ("TAG", "onloading==" + current + "/" + Total + ", isdownloading==" +isdownloading);    }        }); }    Private voidDownloadFile () {requestparams params=NewRequestparams ("Http://vfx.mtime.cn/Video/2016/09/15/mp4/160915092608935956_480.mp4"); //Set Save DataParams.setsavefilepath (Environment.getexternalstoragedirectory () + "/atguigu/480.mp4"); //set Whether you can cancel the download nowParams.setcancelfast (true); //sets whether to automatically name the header informationParams.setautorename (false); //To set a breakpoint for continued transmissionParams.setautoresume (true); Params.setexecutor (NewPriorityexecutor (3,true));//custom thread pool, valid value range [1, 3], set to 3 o'clock, may block picture loading.x.http (). Get (params,NewCallback.progresscallback<file>() {            /*** Callback This method when the download succeeds, and return the downloaded path to it.@paramfile*/@Override Public voidonsuccess (file file) {LOG.E ("TAG", "onsuccess==" +file.tostring ()); Toast.maketext (xutils3netactivity. This, "onsuccess==" +file.tostring (), Toast.length_short). Show (); } @Override Public voidOnError (Throwable ex,Booleanisoncallback) {LOG.E ("TAG", "onerror==" +ex.getmessage ()); } @Override Public voidoncancelled (cancelledexception CeX) {LOG.E ("TAG", "oncancelled==" +cex.getmessage ()); } @Override Public voidonfinished () {LOG.E ("TAG", "onfinished=="); } @Override Public voidonwaiting () {LOG.E ("TAG", "onwaiting=="); } @Override Public voidonstarted () {LOG.E ("TAG", "onstarted=="); } @Override Public voidOnloading (LongTotalLongCurrentBooleanisdownloading) {Progressbar.setmax (int) total); Progressbar.setprogress ((int) (current); LOG.E ("TAG", "onloading==" +current+ "/" +total+ ", isdownloading==" +isdownloading);    }        }); }    Private voidgetandpostnet () {//1.Get Request//2.Post RequestRequestparams params =NewRequestparams ("Http://api.m.mtime.cn/PageSubArea/TrailerList.api"); X.http (). Post (params,NewCallback.commoncallback<string>() {@Override Public voidonsuccess (String result) {LOG.E ("TAG", "XUTIS3 networking request Success = =" +result);//Textview.settext ("Result of Get request ——————" + result);Textview.settext ("Result of Post request ——————" +result); } @Override Public voidOnError (Throwable ex,Booleanisoncallback) {LOG.E ("TAG", "XUtis3 networking request Failed = =" +ex.getmessage ()); Textview.settext ("XUtis3 Networking request Failed = =" +ex.getmessage ()); } @Override Public voidoncancelled (cancelledexception CeX) {LOG.E ("TAG", "oncancelled==" +cex.getmessage ()); } @Override Public voidonfinished () {LOG.E ("TAG", "onfinished==");    }        }); }}

XUTILS3 file Upload, download, get, Post requests

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.