• Okhttp常用方法示例


    这是我用到的一个util类
     1 public class HttpBaseService {
     2 
     3     private OkHttpClient client = new OkHttpClient();
     4     private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
     5     public String sendGetRequest(String url){
     6         Request request = new Request.Builder().url(url).build();
     7         Response response = null;
     8         try {
     9             response = client.newCall(request).execute();
    10             if (response.isSuccessful()) {
    11                 return response.body().string();
    12             }
    13         } catch (IOException e) {
    14             e.printStackTrace();
    15         }
    16         return "";
    17     }
    18 
    19     public int sendDelRequest(String url){
    20         Request request = new Request.Builder().url(url).delete().build();
    21         return sendOperateRequest(request);
    22     }
    23 
    24     public int sendPostRequest(String url,String json){
    25 //        RequestBody formBody = new FormEncodingBuilder()
    26 //                .add("platform", "android")
    27 //                .add("name", "bug")
    28 //                .add("subject", "XXXXXXXXXXXXXXX")
    29 //                .build();
    30         RequestBody body = RequestBody.create(JSON,json);
    31         Request request = new Request.Builder().url(url).post(body).build();
    32         return sendOperateRequest(request);
    33     }
    34     public int sendPutRequest(String url,String json){
    35         RequestBody body = RequestBody.create(JSON,json);
    36         Request request = new Request.Builder().url(url).put(body).build();
    37         return sendOperateRequest(request);
    38     }
    39 
    40     private int sendOperateRequest(Request request){
    41         Response response = null;
    42         try {
    43             response = client.newCall(request).execute();
    44             if (response.isSuccessful()) {
    45                 return 0;
    46             }
    47         } catch (IOException e) {
    48             e.printStackTrace();
    49         }
    50         return -1;
    51     }
    52 }
    
    
    
     
  • 相关阅读:
    socket.io
    CUDA升级后
    QT安装
    windows时钟服务设置
    QT的DPI支持
    cudaDeviceProp结构体
    C#调用C++的dll各种传参
    「LibreOJ#516」DP 一般看规律
    「LibreOJ#515」贪心只能过样例 (暴力+bitset)
    [Codeforces888E]Maximum Subsequence(暴力+meet-in-the-middle)
  • 原文地址:https://www.cnblogs.com/yoyotl/p/6864231.html
Copyright © 2020-2023  润新知