GitHub Address
' cn.yuan.yu:library:1.0.2 '
First step: Initialize our tool class
Public classMyApplication extends Application {Private StaticHandler MainHandler; Private StaticContext instance; @Override Public voidonCreate () {super.oncreate (); Instance= This; MainHandler=NewHandler (); /** Initialize Okhttputils * The first parameter is whether to submit the form of JSON data or form * The second parameter is the identity of the request returned code * The third parameter is the correct co to be returned by the request The de value * The fourth parameter is the prompt message returned by the request key * The fifth parameter is to synchronize the thread's problem * The sixth parameter is the context that pops up the hint message*/okhttputilsconfig.getinstance (). Init (false,"ResultCode"," -","msg", MainHandler, This); }}
Public classMyApplication extends Application {Private StaticHandler MainHandler; Private StaticContext instance; @Override Public voidonCreate () {super.oncreate (); Instance= This; MainHandler=NewHandler (); /** Initialize Okhttputils * The first parameter is whether to submit the form of JSON data or form * The second parameter is the identity of the request returned code * The third parameter is the correct co to be returned by the request The de value * The fourth parameter is the prompt message returned by the request key * The fifth parameter is to synchronize the thread's problem * The sixth parameter is the context that pops up the hint message*/okhttputilsconfig.getinstance (). Init (false,"ResultCode"," -","msg", MainHandler, This); }}
The second step, the direct call to use, is so simple rough
Jsonobject Jsonobject =NewJsonobject (); Jsonobject.put ("username", username); Jsonobject.put ("Password", PWD); L.D ("Login", jsonobject.tojsonstring ()); Requestpacket Requestpacket=NewRequestpacket (); Requestpacket.url=Constanturl.getbaseurl (); Requestpacket.addargument ("Data", jsonobject.tojsonstring ()); Requestpacket.addargument ("Query","Login"); Okhttputil.request (Requestpacket.post,requestpacket,NewResponselistener<registgetvcodebean>() {@Override Public voidonsuccess (Registgetvcodebean registgetvcodebean) {//Todo somethiing} @Override Public voidonfailure (String responseresult) {//Todo somethiing } });
Registgetvcodebean
Public class Registgetvcodebean {}
The source of this article: 62897368
Okhttp packaging and use of a detailed