package com.muyu.warning.util;

import okhttp3.*;

import java.io.IOException;

public class OkHttpUtils {

    private static final OkHttpClient client = new OkHttpClient();

    public static String sendGetRequest(String urlString) {
        Request request = new Request.Builder()
                .url(urlString)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String sendPostRequest(String urlString, String params) {
        RequestBody requestBody = RequestBody.create(params, MediaType.parse("application/json; charset=utf-8"));
        Request request = new Request.Builder()
                .url(urlString)
                .post(requestBody)
                .build();
        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
