The
Okhttp supports asynchronous HTTP request invocations in addition to the commonly used synchronous HTTP requests. When a synchronous call is used, the current thread is blocked until the HTTP request completes. When multiple HTTP requests are issued simultaneously, the performance of the synchronization call is poor. This time through asynchronous invocation can improve the overall performance.
After a new call object is created through the Newcall method, it is not performed synchronously through the Execute method, but is added to the execution queue by the Enqueue method. You need to provide an implementation of the Callback interface when calling the Enqueue method. In the Callback interface implementation, the Onresponse and OnFailure methods are used to handle the response and error handling.
An asynchronous call to the example
public class Asyncget {public
static void Main (string[] args) throws IOException {
okhttpclient client = new Okht Tpclient ();
Request Request = new Request.builder ()
. URL ("http://www.baidu.com")
. Build ();
Client.newcall (Request). Enqueue (New Callback () {public
void OnFailure (Request request, IOException e) {
E.printstacktrace ();
}
public void Onresponse (Response Response) throws IOException {
if (!response.issuccessful ()) {
throw new IOException ("Server-side error:" + response);
}
System.out.println (Response.body (). String ());}}
Think okhttp the hardest place to write should be callback.
I believe many people will encounter, if callback back, my activity finish, or my fragment replace.
Changing the UI at this point can cause a view problem to be found.
And callback came back, incredibly on the Backgroundthread,
This time if you want to operate view and switch to Mainthread, slightly trouble.
So I am writing, is not using the following callback to provide a beautiful (admission ... ) as a reference for everyone to do.
/* Take Fragment as an example */public class Basefragment extends Fragment implements Handler.callback {private static final int MSG
_query_data = 0x00;
private static final int msg_display_data = 0x01;
@Override public void Onattach [activity activity] {Super.onattach (activity);
This.activity = activity;
/* Setup handler/handlerthread Handlerthread = new Handlerthread (GetClass (). GetName ());
Handlerthread.start ();
Backgroundhandler = new Handler (Handlerthread.getlooper (), this);
Uihandler = new Handler (Activity.getmainlooper (), this); @Override public void onviewcreated (view view, @Nullable Bundle savedinstancestate) {/* start */backgroundhandle
R.sendemptymessage (Msg_query_data); @Override public void Ondestroyview () {/* will clear the message, Backgroundthread end off/backgroundhandler.removecallbacksandm
Essages (NULL);
Uihandler.removecallbacksandmessages (NULL);
Backgroundhandler.getlooper (). Quit ();
Super.ondestroyview (); @Override public boolean handlemessage (MesSage msg) {/* If fragment is not on the activity, direct return to avoid NPE * * (!isadded ()) return false; /* Make all kinds of msg/switch (msg.what) {case Msg_query_data://Do okhttp without callback Response Response =
Client.newcall (Request). Execute ();
Send back Uithread do UI update message respmsg = Uihandler.obtainmessage ();
Respmsg.what = Msg_display_data;
Respmsg.obj = response;
Backgroundhandler.sendmessage (RESPMSG);
Break
Case Msg_display_data:response Apiresponse = (Response) msg.obj;
Failure if (null = = Apiresponse) {//show Error}//success else{//display data on UI
} break;
return false;
}
}