直接上代码
// post请求body LoginParam loginParam = new LoginParam("18622882395", "123456", "140fe1da9e9de91cbff", 1); Gson gson = new Gson(); // 转换层json字符串 final String s = gson.toJson(loginParam); // 创建 RequestBody RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), s); // 获取OkhthpClient 实例 OkHttpClient okHttpClient1 = OkHttpUtils.getInstance().getOkHttpClient(); // 创建请求 Request request = new Request.Builder().url("http://url") .post(requestBody).build(); // 请求网络 okHttpClient1.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.e("sss", "onFailure"); } @Override public void onResponse(Call call, Response response) throws IOException { String string = response.body().string(); Log.e("sss", string); } });