okhttp post json example 2019-04-28 05:55

okhttp is one of best network framework. In this page I will show your how to use it post json to remote server. The code is here.

public class OkHttpExampleClient {
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static void main(String[] args) throws IOException {

        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(JSON, "{\"name\":\"henry\",\"age\":30}");
        Request request = new Request.Builder()
        Response response = client.newCall(request).execute();

You need add dependency in your pom file.