<span style= "font-family:arial, Helvetica, Sans-serif; Background-color:rgb (255, 255, 255); " ></span><pre name= "code" class= "java" >package com.bruce.okhttpdemo;import android.app.Activity; Import Android.os.bundle;import android.util.log;import Android.widget.imageview;import Java.io.IOException;import OKHTTP3. Call;import OKHTTP3. Callback;import OKHTTP3. Mediatype;import OKHTTP3. Okhttpclient;import OKHTTP3. Request;import OKHTTP3. Requestbody;import OKHTTP3. Response;import Okio. bufferedsink;/** * compile ' com.squareup.okhttp3:okhttp:3.2.0 ' */public class Mainactivity extends Activity {ImageView img @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); img = (ImageView) Findviewbyid (r.id.img); Getstringbyokhttpget (); }/** * 1-above is the process of sending a GET request, first constructs a requests object, the parameter at least has a URL, * Of course you can set more parameters through Request.builder such as: header, method, etc. 2-The Request object is then constructed to get a call pairLike, similar to encapsulating your request as a task, since it is a task, there are methods such as execute () and cancel (). 3-Finally, we want to execute the request asynchronously, so we're calling Call.enqueue, adding call to the dispatch queue, and waiting for the task to finish, we can get the result in callback. *//** * A simple GET request (default is GET request) */private void Getstringbyokhttpget () {//Create Okhttpclient Object Okht Tpclient mokhttpclient = new Okhttpclient (); Create a request final request request = new Request.builder (). URL ("https://github.com/hongyangAndroid "). Build (); Create a new request-call calls call = Mokhttpclient.newcall (request); This request is then added to the dispatch queue Call.enqueue (new Callback () {@Override public void onfailure (called Call, Ioex Ception e) {LOG.W ("onfailure", "" "+ e.tostring ()); /** * Onresponse Callback parameter is response, generally, for example, we want to get the returned string, can be obtained by the Response.body (). String (); If you want to get the returned binary byte array, call Response.body (). bytes (); If you want to get the returned InputStream, call Response.body (). ByteStream () See this, you may wonder, unexpectedly You can still get the inputst back.Ream, see this at least can realize a little, here support large file download, there is InputStream we can write the file by IO way. However, it also shows that the thread that this onresponse executes is not the UI thread. Yes, if you want to manipulate the controls, you need to use handler, etc., * @param call * @param response * @throws IOException */@Override public void Onresponse (call call, Response Response) throws IOException { String htmlstr = Response.body (). String (); LOG.W ("Onresponse", "" "+ Response.body (). String ()); } }); } private void Getstringbypost () {okhttpclient mokhttpclient = new Okhttpclient (); Requestbody requestbody = new Requestbody () {@Override public mediatype ContentType () { return null; } @Override public void WriteTo (Bufferedsink sink) throws IOException {}}; Request Request = new Request.builder (). URL ("www.baidu.com"). Post (Requestbody) . build (); Mokhttpclient.newcall (Request) Enqueue (new Callback () {@Override public void onfailure Oexception e) {} @Override public void Onresponse (call call, Response Response) throws I Oexception {}}); } private static Final mediatype Media_type_markdown = Mediatype.parse ("Text/x-markdown; Charset=utf-8 "); Private final Okhttpclient client = new Okhttpclient (); public void Run () throws Exception {String postbody = "" + "releases\n" + "--------\ n "+" \ n "+" * _1.0_ May 6, 2013\n "+" * _1.1_ June, 2013\n " + "* _1.2_ August One, 2013\n"; Request Request = new Request.builder (). URL ("Https://api.github.com/markdown/raw"). Post (Re Questbody.create (Media_type_markdown, Postbody)). Build (); Response RESponse = Client.newcall (Request). Execute (); if (!response.issuccessful ()) throw new IOException ("Unexpected Code" + response); System.out.println (Response.body (). String ()); public static final MediaType JSON = Mediatype.parse ("Application/json; Charset=utf-8 "); String post (string URL, string json) throws IOException {Requestbody BODY = requestbody.create (JSON, JSON); Request Request = new Request.builder (). URL (URL). Post (body). Build (); Response Response = client.newcall (Request). Execute (); Return Response.body (). String (); }}
Zhang Honyang Blog
Official documents
First time use of okhttp