1、在app/build.gradle中添加依赖
compile ‘com.squareup.okhttp3:okhttp:4.0.1‘
2、创建OkHttpClient实例
OkHttpClient client = new OkHttpClient();
3、发送HTTP请求
Request request = new Request.Builder().build();
上述只是创建了一个空的Request,可以在.build()方法前连缀很多其他方法。
4、发送请求,并获取服务器返回的数据
Response response = client.newCall(request).execute();
5、显示返回的内容
String responseData = response.body().string();
POST:
RequestBody resquestBody = new FormBody.Builder().add("username","admin").add("password","123456").build();
Request request = new Request.Builder().url("htpp://www.baidu.com").post(requestBody).build();
时间: 2024-10-06 12:02:20