A little experience about OKHttp

1. Overview
        okhttp is an http client that focuses on improving the efficiency of network connections. The Android version of the Http client supports SPDY, connection pooling, GZIP and HTTP caching;

It can reuse a socket for requests of the same ip and port, which can greatly reduce the network connection time. Compared with the method of establishing a socket for each request and then disconnecting the socket, it reduces the pressure on the server.

2. Cases based on json requests

   public static String postWithJson(String jsonStr,String url,String clientId){
        String result="";
        MediaType mediaType = MediaType.parse("application/json;charset=utf-8");
        RequestBody body = RequestBody.create(mediaType, jsonStr);
        Request request = new Request.Builder()
            .url(url)
            .post(body)
            .addHeader("content-type", "application/json;charset=utf-8")
            .addHeader("X-SPDB-Client-Id", clientId).build();
        try{
            Response response = client.newCall(request).execute();
            result=new String(response.body().bytes(), "UTF-8");
        }catch(IOException e){
            e.printStackTrace();
        }
        return result;
    }


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324739600&siteId=291194637