Android Open source Framework (ii) Introduction of okhttp-utils based on okhttp further encapsulation

Source: Internet
Author: User

Okhttp-utils is Zhang Hongyang is a framework library based on the Okhttp package. The actual work, not much use, for small projects of network requests and file transfer can be considered for direct use. Otherwise, it is based on the mainstream Okhttp+retrift+rxjava framework.

For okhttp use, you can do some encapsulation according to your own project needs. If you are dealing with a temporary project at hand, you can draw on GitHub for some open source Okhttp package libraries. Just go to GitHub and search under Okhttp will come out many packaged frames.

Analysis Okhttp-utils use, just for your own learning to understand.

I. Introducing Okhttp-utils as module into the project

    

Two. Okhttp-utils introduced into the project file list directory:

        

Three. Okhttp-utils basic use 1. GET Request data
okhttputils        . Get ()        . URL (URL).        ID (+).        build ()        . Execute (new Mystringcallback ());
2.//post Request Data
okhttputils.        poststring ()        . URL (URL)        . MediaType (Mediatype.parse ("application/ Json Charset=utf-8")        . Content (new Gson (). ToJson (new User (" Zhy " " 123 " ))         . Build ()        . Execute (new mystringcallback ());        
3.//Uploading Files
okhttputils        . Postfile ()        . URL (url).        file (file)        . Build ()        . Execute (new Mystringcallback ());
4.//Loading Pictures
okhttputils.Get(). URL (url). Tag ( This). Build (). Conntimeout (20000). ReadTimeout (20000). WriteTimeout (20000). Execute (NewBitmapcallback () {@Override Public voidOnError (call call, Exception E,intID) {Mtv.settext ("OnError:"+e.getmessage ()); } @Override Public voidOnresponse (Bitmap Bitmap,intID) {LOG.E ("TAG","Onresponse:complete");            Mimageview.setimagebitmap (bitmap);    }        }); 
5.//uploading a single file
Okhttputils.post ()//        . AddFile ("mfile""messenger_01.png  ", file)        . URL (url)         . params (params) // with form data         . Headers (headers)        . Build ()        . Execute (new mystringcallback ());    
6.//Uploading multiple files
 okhttputils.post (). AddFile (  " mfile   ", "  Span style= "COLOR: #800000" >messenger_01.png   "   " mfile  " ,  " test1.txt  "  , file2). URL (URL) . params  (params ) //   Build (). Execute ( new  mystringcallb    ACK ()); 
7.//Download File
okhttputils.Get(). URL (URL). Build (). Execute (NewFilecallback (Environment.getexternalstoragedirectory (). GetAbsolutePath (),"Gson-2.2.1.jar") {@Override Public voidOnbefore (Request request,intID) {} @Override Public voidInProgress (floatProgressLongTotalintID) {//Progress bar DisplayMprogressbar.setprogress ((int) ( -*progress)); LOG.E (TAG,"inProgress:"+ (int) ( -*progress)); } @Override Public voidOnError (call call, Exception E,intID) {LOG.E (TAG,"OnError:"+e.getmessage ()); } @Override Public voidOnresponse (File file,intID) {LOG.E (TAG,"Onresponse:"+File.getabsolutepath ());    }        }); 
The public callback method defined:
 Public classMystringcallback extends Stringcallback {@Override Public voidOnbefore (Request request,intID) {} @Override Public voidOnafter (intID) {} @Override Public voidOnError (call call, Exception E,intID) {e.printstacktrace (); } @Override Public voidOnresponse (String response,intID) {LOG.E (TAG,"Onresponse:complete"); Mtv.settext ("Onresponse:"+response); Switch(ID) { Case  -: Toast.maketext (mainactivity. This,"http", Toast.length_short). Show ();  Break;  Case 101: Toast.maketext (mainactivity. This,"HTTPS", Toast.length_short). Show ();  Break; }} @Override Public voidInProgress (floatProgressLongTotalintID) {LOG.E (TAG,"inProgress:"+progress); Mprogressbar.setprogress ((int) ( -*progress)); }    }        

Android Open source Framework (ii) Introduction of okhttp-utils based on okhttp further encapsulation

Related Article

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.