The recent comparison of Fire is Okhttp+picasso analytic mixed use
Next we'll grab the data okhttp.
显然Okhttp里面的那个onResponse 和 onFailure是非UI线程,那么我们改变他的话只有发送个通知出来让其他的UI线程来处理UI变化了,这个时候,屌丝王Handler出场了。
1.okhttp GET request Encapsulation
Directly on the code:
Public Static Final intOk_error =-1; Public Static Final intOk_success =1;/** * Okhttp Post Request method */ Public Static void Okdoget(String URL,FinalHandler Handler) {okhttpclient client =NewOkhttpclient ();FinalRequest Request =NewRequest.builder (). URL (URL). build ();//New callCall call = Client.newcall (request); Call.enqueue (NewCallback () {@Override Public void onfailure(Request request, IOException e) {handler.sendemptymessage (ok_error); }@Override Public void Onresponse(FinalResponse Response)throwsIOException {String resultstr = Response.body (). String (); Message msg =NewMessage (); Msg.what = ok_success; Msg.obj = ResultStr; Handler.sendmessage (msg); } }); }
As for the handler inside how to take self-brain repair.
2.Okhttp POST Request Encapsulation
/** * Package * for POST request * / Public Static void Okdopost(String URL,FinalHandler Handler, Formencodingbuilder builder) {Okhttpclient client =NewOkhttpclient (); Request Request =NewRequest.builder (). URL (URL). Post (Builder.build ()). build (); Client.newcall (Request). Enqueue (NewCallback () {@Override Public void onfailure(Request arg0, IOException arg1) {handler.sendemptymessage (ok_error); }@Override Public void Onresponse(Response arg0)throwsIOException {String resultstr = Arg0.body (). String (); Message msg =NewMessage (); Msg.what = ok_success; Msg.obj = ResultStr; Handler.sendmessage (msg); } }); }/** * Package * for POST request * / Public Static void Okdopostwithcode(String URL,FinalHandler Handler, Formencodingbuilder Builder,Final intSuccessFinal intError) {Okhttpclient client =NewOkhttpclient (); Request Request =NewRequest.builder (). URL (URL). Post (Builder.build ()). build (); Client.newcall (Request). Enqueue (NewCallback () {@Override Public void onfailure(Request arg0, IOException arg1) {handler.sendemptymessage (Error); }@Override Public void Onresponse(Response arg0)throwsIOException {String resultstr = Arg0.body (). String (); Message msg =NewMessage (); Msg.what = success; Msg.obj = ResultStr; Handler.sendmessage (msg); } }); }
Because he passed the value is uncertain, so I formencodingbuilder to give out to add, so how to preach how to preach, and do not know if he is a success or failure, so he made a lot of two judgments.
Parameter methods for 3.post requests
/** * POST request * /Formencodingbuilder Builder =NewFormencodingbuilder (); Builder.add ("sj_id","1"); Builder.add ("XX","1"); Builder.add ("Page", page +""); Builder.add ("Oauth_token","f5fbefc2bd9f538c3010fd24f57eb536"); Okhttputil.okdopost (Contexturl.url, Handler, builder);//post Request Method Invocation /** * POST request with request code * /Formencodingbuilder Builder =NewFormencodingbuilder (); Builder.add ("sj_id","1"); Builder.add ("XX","1"); Builder.add ("Page", page +""); Builder.add ("Oauth_token","f5fbefc2bd9f538c3010fd24f57eb536"); Okhttputil.okdopostwithcode (Contexturl.url, handler, builder,3,4);
Okhttp for the time being here, what to add again.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Okhttp Package Use