android okhttp 示例
时间: 2023-07-21 08:52:26 浏览: 117
android_okhttp3_example-master_okhttp_androidapp_源码
以下是一个使用OkHttp进行GET请求的示例代码:
```java
OkHttpClient client = new OkHttpClient();
String url = "http://www.example.com";
Request request = new Request.Builder()
.url(url)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
```
以下是一个使用OkHttp进行POST请求的示例代码:
```java
OkHttpClient client = new OkHttpClient();
String url = "http://www.example.com";
RequestBody requestBody = new FormBody.Builder()
.add("param1", "value1")
.add("param2", "value2")
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
Response response = client.newCall(request).execute();
String responseData = response.body().string();
```
注意:以上示例代码仅为演示使用,实际使用时需要根据具体情况进行适当修改,如添加异常处理等。
阅读全文