1, the Okhttp framework uses the Okio framework, do not forget the next Okio.jar
2, through synchronous get access to the network, mainly divided into four steps:
(1), declare and instantiate a Okhttpclient object.
(2), declare and instantiate a request object, and set parameters such as URLs.
(3), execute request requests, and obtain a response object.
(4), according to the response Issuccessful () method, determine whether the success, and then get the return data from the response object.
3, through the asynchronous get access to the network and synchronous get is the same, but the processing of the return data is actually implemented in the callback interface.
4.
Public classGetactivityextendsActivity {Private FinalOkhttpclient client =Newokhttpclient (); PrivateTextView Mtvget; PrivateString result; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_get); Initview (); NewThread (GetThread). Start (); } Public voidInitview () {Mtvget=(TextView) Findviewbyid (r.id.tv_get_show); } PrivateRunnable GetThread =NewRunnable () { Public voidrun () {Try{getRun2 (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } }; }; /**Synchronize Get*/ Public voidGetrun ()throwsException {//instantiating a Request objectRequest Request =NewRequest.builder (). URL ("https://www.baidu.com/"). build (); //get returned objectResponse Response =Client.newcall (Request). Execute (); //determine if the returned object is successful if(!response.issuccessful ()) {Result+ = "Request Error!"; } Else { //Output HeadersHeaders responseheaders =response.headers (); for(inti = 0; I < responseheaders.size (); i++) {result+ = Responseheaders.name (i) + "" +Responseheaders.value (i); } //Output BodyResult + =response.body (). String (); } gethandler.sendemptymessage (0); } /**Asynchronous Get*/ Public voidGETRUN1 ()throwsException {Request Request=NewRequest.builder (). URL ("https://www.baidu.com/"). build (); Client.newcall (Request). Enqueue (NewCallback () {@Override Public voidOnresponse (Response Response)throwsIOException {//determine if the returned object is successful if(!response.issuccessful ()) {Result+ = "Request Error!"; } Else { //Output HeadersHeaders responseheaders =response.headers (); for(inti = 0; I < responseheaders.size (); i++) {result+ = Responseheaders.name (i) + "" +Responseheaders.value (i); } //Output BodyResult + =response.body (). String (); } gethandler.sendemptymessage (0); } @Override Public voidonfailure (Request arg0, IOException e) {//TODO auto-generated Method StubE.printstacktrace (); } }); } PrivateHandler GetHandler =NewHandler () { Public voidhandlemessage (Message msg) {Mtvget.settext (result); }; };}
5, access to the network can not be in the main thread, and do not forget to join the access network permissions.
<uses-permission android:name= "Android.permission.INTERNET"/>
6. Reference Blog:
Http://www.2cto.com/kf/201505/397557.html
http://blog.csdn.net/lmj623565791/article/details/47911083
7, for the request object is how to instantiate, we can refer to--java Builder mode
Http://www.cnblogs.com/moonz-wu/archive/2011/01/11/1932473.html
okhttp--basic usage of Android Web development get