Android development, the inevitable use of network technology, and in most cases, we are using the HTTP protocol to send and receive network data. The Android system offers two main ways of HTTP communications, HttpURLConnection and httpclient, but Google recommends using httpurlconnection from Android 2.3 and beyond, The fact that the httpclient API is too large makes it difficult to upgrade and extend it without breaking compatibility, so the Android team is not actively working on upgrading and optimizing httpclient. HttpURLConnection is a versatile, lightweight HTTP client that can be used for HTTP operations to apply to most applications. Although the HttpURLConnection API provides a simpler, it also makes it easier to use and extend it.
But it is because of this, the use of httpurlconnection is more complex, if not properly encapsulated, it is easy to write a lot of duplicate code, so, some of the Android network communication framework has emerged, today to talk about is the Okhttp open source framework.
Okhttp can do a lot of things, including uploading strings, uploading files, uploading streams, uploading table parameters, uploading multiple requests, responding to Json, responding to caching, and so on. The main popular JSON data communication is currently, so we'll talk about get and POST requests and responses based on JSON communication.
Environmental preparedness
Introduced so much theoretical knowledge, then enter the actual combat phase, first download okhttp jar package, you can go to GitHub download the most recent package.
This is the latest download address: Https://search.maven.org/remote_content?g=com.squareup.okhttp3&a=okhttp&v=LATEST
Of course, you can also add compilations directly to your project (for Android Studio): Compile ' com.squareup.okhttp3:okhttp:3.2.0 '
Okhttp's project address: https://github.com/square/okhttp
In addition, you need to add a okhttp dependency pack: Okio.jar, download address: Https://search.maven.org/remote_content?g=com.squareup.okio&a=okio &v=latest
Project Address: Https://github.com/square/okio
Compile address: Compile ' com.squareup.okio:okio:1.6.0 '
Example
1. Request a URL
Here The example requests a URL and prints the content in a string format, the main code:
Okhttpclient client = new Okhttpclient ();
String run (string url) throws IOException {
Request request = new Request.builder ()
. URL (URL)
. Build ();
Response Response = client.newcall (Request). Execute ();
Return Response.body (). String ();
2. Post requests to the server
Send a POST request to the server, main code:
public static final mediatype JSON
= Mediatype.parse ("Application/json; Charset=utf-8 ");
Okhttpclient client = new Okhttpclient ();
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 ();