Okhttp常用方法示例

这是我用到的一个util类
 public class HttpBaseService {

     private OkHttpClient client = new OkHttpClient();
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
public String sendGetRequest(String url){
Request request = new Request.Builder().url(url).build();
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
return response.body().string();
}
} catch (IOException e) {
e.printStackTrace();
}
return "";
} public int sendDelRequest(String url){
Request request = new Request.Builder().url(url).delete().build();
return sendOperateRequest(request);
} public int sendPostRequest(String url,String json){
// RequestBody formBody = new FormEncodingBuilder()
// .add("platform", "android")
// .add("name", "bug")
// .add("subject", "XXXXXXXXXXXXXXX")
// .build();
RequestBody body = RequestBody.create(JSON,json);
Request request = new Request.Builder().url(url).post(body).build();
return sendOperateRequest(request);
}
public int sendPutRequest(String url,String json){
RequestBody body = RequestBody.create(JSON,json);
Request request = new Request.Builder().url(url).put(body).build();
return sendOperateRequest(request);
} private int sendOperateRequest(Request request){
Response response = null;
try {
response = client.newCall(request).execute();
if (response.isSuccessful()) {
return ;
}
} catch (IOException e) {
e.printStackTrace();
}
return -;
}
}

 
上一篇:阿里云RDS读写分离数据查询延迟解决


下一篇:初学Android 一 基本开发环境