Best HTTP request Framework to date

Source: Internet
Author: User
Tags throwable

This article original, reprint please indicate link http://blog.kymjs.com/
Kjframeforandroid:https://github.com/kymjs/kjframeforandroid

Basic functions

Kjhttp is specifically designed to address the HTTP communication in Android, and it is fully automated to build and parse at the request and response level, primarily for Android rapid development.
Kjhttp comes with the data cache feature, the HTTP data you access will be locally created a cache, the default cache time is 5 minutes, that is, within 5 minutes the same request will not go through the network, but from the local cache directly read. Of course, for high-timeliness news applications, you can also turn off this feature or just set the cache time to 0.
KJHTTP supports JSON format parameter submission, Ajax mode form parameter submission, file and image upload and download, and supports custom request based on your needs.

Working principle

The whole kjhttp work flow: Adopt the responsibility chain design pattern, consist of three parts, similar design can analogy handle ... Looper ... MessageQueue
1. Kjhttp is responsible for constantly networkqueue (or cachequeue actual or networkqueue, specific logic see {@link cachedispatcher}) Add request
2. On the other side, from the Taskthread, the request is constantly taken from the networkqueue and handed to the network actuator (see {@link networkdispatcher} for logic).
3. The network executor returns the successful networkresponse to Taskthead and encapsulates it into response via the request's custom method {@link request#parsenetworkresponse ()}. It is eventually handed to the dispatcher {@link Delivery} to distribute to the main thread and call Httpcallback the appropriate method

Base usage get or POST request Data sample as JSON parameter
 /** * Using JSON to submit parameters instead of form forms */  Private void jsonrequest() {Kjhttp KJH =NewKjhttp (); Httpparams params =NewHttpparams (); Params.putheaders ("Cookie","Cookies cannot tell you");//The JSON string is passed here, (Jsonobject can call the ToString Method transformation)Params.putjsonparams (Jsonobj.tostring ()); Kjh.jsonpost ("Http://www.oschina.net/action/api/team_stickynote_batch_update"ParamsNewHttpcallback () {@Override                   Public void onsuccess(String t) {Super. onsuccess (t);                  Toast (t);  }              }); }
Get or POST requests JSON data samples in AJAX mode
Kjhttp KJH =NewKjhttp (); Httpparams params =NewHttpparams (); Params.put ("id","1");//Pass -through parametersParams.put ("Name","Kymjs");There are many methods in//httpcallback that can be implemented according to requirementsKjh.post ("Http://192.168.1.149/post_api"ParamsNewHttpcallback () {@Override       Public void onsuccess(String t) {Super. onsuccess (t); Viewinject.longtoast ("Request succeeded"); Kjloger.debug ("LOG:"+ t.tostring ()); }// ......      //There are more, do not introduce each one, you can refer to the API documentation      // ......});
File upload support multi-file upload, support for files object, byte[] Two
  Private void Upload() {Httpparams params =NewHttpparams ();//Can be put multiple times, support multi-file uploadParams.put ("File", Fileutils.getsavefile ("Kjlibrary","Logo.jpg")); Kjh.post ("http://192.168.1.149/kymjs/hello.php"ParamsNewHttpcallback () {@Override                   Public void onsuccess(String t) {Super. onsuccess (t); Viewinject.toast ("Success"); }@Override                   Public void onfailure(Throwable T,intErrorno, String strMsg) {Super. OnFailure (t, Errorno, STRMSG); Viewinject.toast ("Error"+ STRMSG); }/** There are more implementations ... **/}); }
File download and breakpoint continuation
Kjh.download ("http://www.kymjs.com/app/kjblog.apk","file.apk",NewHttpcallback () {@Override         Public void onsuccess(File f) {Super. onsuccess (f); Kjloger.debug ("Success"); Viewinject.toast ("Toast");        Mprogress.setprogress (Mprogress.getmax ()); }@Override         Public void onfailure(Throwable T,intErrorno,string strMsg) {Super. OnFailure (t, Errorno, STRMSG); Kjloger.debug ("OnFailure"); }/ * The Onloading method will only be recalled when the file is downloaded * /        @Override         Public void onloading(LongCountLongCurrent) {Super. onloading (count, current); Mprogress.setmax ((int) count); Mprogress.setprogress ((int) (current); Kjloger.debug (Count +"------"+ current); }    });
More Configurable methods
new KJHttp();//取消一个请求kjh.cancle(url);//读取一个本地缓存数据kjh.getCache(url);//删除一个本地缓存kjh.removeCache(url);
Advanced Settings

The above is the basic usage, also supports custom settings, you can customize the way the data is cached, the priority of the request, the time to request the data. It also supports custom requests that are initiated according to your needs.
More use, can refer to the actual project in the use of love to see the blog client

Best HTTP request Framework to date

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.