OkHttp之post请求发送给服务器json

来源:互联网 发布:带网络变压器的rj45 编辑:程序博客网 时间:2024/05/18 02:49
public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");    public static String MypostJson(String api, Object RequestJsonbean) throws IOException {        /**         * 返回的仍然是json格式         */        Gson gson = new Gson();        String json = gson.toJson(RequestJsonbean);        OkHttpClient client = new OkHttpClient();        RequestBody body = RequestBody.create(JSON, json);        Request request = new Request.Builder().url(api).post(body).build();        Response response = client.newCall(request).execute();        if (response.isSuccessful()) {            return response.body().string();        } else {            throw new IOException("Unexpected code " + response);        }    }
阅读全文
0 0
原创粉丝点击