1, the Okhttp framework uses the Okio framework, do not forget the next Okio.jar
2, through post access to the network, and through get access to the network basically the same, more than the process of setting request parameters. It is divided into five main steps:
(1), Declaration and instantiation of a Okhttpclient object
(2), Declaration and instantiation of a Requestbody object
(3), declaring and instantiating a request object
(4), execute request requests, and get a response object
(5), according to Response's Issuccessful () method to determine whether the success, and then get the return data from the response object.
3.
Public classPostactivityextendsActivity {PrivateOkhttpclient client =Newokhttpclient (); PrivateTextView Mtvpost; PrivateString url = "https://www.baidu.com/"; PrivateString result = ""; @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_post); Initview (); NewThread (Postthread). Start (); } Public voidInitview () {mtvpost=(TextView) Findviewbyid (r.id.tv_post_show); } PrivateHandler Posthandler =NewHandler () { Public voidhandlemessage (android.os.Message msg) {Mtvpost.settext (result); }; }; PrivateThread Postthread =NewThread () { Public voidrun () {Try{run (); } Catch(Exception e) {//TODO auto-generated Catch blockE.printstacktrace (); } posthandler.sendemptymessage (0); }; }; /**Posting a String*/ Public Static FinalMediaType jsonreq = Mediatype.parse ("Application/json;charset=utf-8"); Public voidRun ()throwsIOException {requestbody body= Requestbody.create (Jsonreq, "{\" name\ ", \" Name\ "}"); Request Request=Newrequest.builder (). URL (URL). Post (body). Build (); Response Response=Client.newcall (Request). Execute (); Result+=response.body (). String (); } /**Posting a File*/ Public Static FinalMediaType media_type_markdown1 = Mediatype.parse ("Text/x-markdown;charset=utf-8"); Public voidRun2 ()throwsException {File file=NewFile (Environment.getexternalstoragedirectory (). GetPath () + "/dcim/camera/img_20151030_205855.jpg"); Request Request=Newrequest.builder (). URL (URL). Post (Requestbody.create (Media_type_markdown, file)). build (); Response Response=Client.newcall (Request). Execute (); if(!response.issuccessful ()) {Result+ = "Error" +response; }Else{result+ = "No error" +response.body (). String (); } } /**Posting from Parameters*/ Public voidRUN3 ()throwsException {requestbody formbody=NewFormencodingbuilder (). Add ("Search", "Jurassic Park"). build (); Request Request=Newrequest.builder (). URL (URL). Post (Formbody). build (); Response Response=Client.newcall (Request). Execute (); if(!response.issuccessful ()) {Result+ = "Something went wrong"; }Else{result+=response.body (). toString (); } } /**posing Json with Gson*/ Private FinalGson Gson =NewGson (); Public voidRun5 ()throwsException {Request Request=Newrequest.builder (). URL (URL). build (); Response Response=Client.newcall (Request). Execute (); if(!response.issuccessful ()) {Result+ = "Something went wrong"; }Else{Gist Gist= Gson.fromjson (Response.body (). Charstream (), Gist.class); for(Map.entry<string, gistfile>Entry:gist.files.entrySet ()) {Result+ = Entry.getkey () +entry.getvalue (). Content; } } } Static classGist {Map<string, gistfile>files; } Static classGistfile {String content; }}
4, 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"/>
5. Reference Blog:
Http://www.2cto.com/kf/201505/397557.html
http://blog.csdn.net/lmj623565791/article/details/47911083
6, 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 post