OkHttp3设置Content-Type

private void connectToApi(String requestMsg, String bizCode) throws IOException {

        try {
            OkHttpClient okHttpClient = this.createClient();

            Logger.t("request").d(url.toString());

            /**
             * 创建请求的参数body
             */
            RequestBody body = FormBody.create(MediaType.parse("application/json"), requestMsg);

            Request request = new Request.Builder()
                    .url(url)
                    .post(body)
                    .build();

            Response response = okHttpClient.newCall(request).execute();
            if (response.isSuccessful()) {
                this.response = response.body().string();
            } else {
                throw new IOException("无法连接到服务器,请检查网络连接");
                //throw new IOException("Unexpected code " + response);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new IOException("网络有点卡...");
        }
    }

你可能感兴趣的:(Android)