okhttp-utils使用

okhttp-utils使用

学习鸿洋的okhttp-utils,这边对各类请求方式作个总结:
具体见:hongyangAndroid/okhttputils okhttputilsgit


Get

  • 无需带参
OkHttpUtils
          .get()
          .url("xxxxx")
          //项目须要就设置,不须要就不用写
          .addHeader("Authorization", xxxxx)
          .build()
          .execute(new StringCallback() {
           @Override
           public void onError(Call call, Exception e, int id,   int code)                
           {
                        
                   
            }

            @Override
            public void onResponse(String response, int id, int code)      {
                       
           }
           });
  • 需带参数
OkHttpUtils
          .get()
          .url("xxxxx")
          .addHeader("Authorization", xxxxx)
           //添加参数
          .addParams("xxxxx",xxxxx)
          .addParams("xxxxx", xxxxx)
          .build()
          //这里callback直接解析数据
          //new GenericsCallback<你要解析的Bean类名>
          .execute(new GenericsCallback<FollowChangeListBean>(new JsonGenericsSerializator()) 
           {
           @Override
           public void onError(Call call, Exception e, int id, int code) 
           {
          
           }
          @Override
          public void onResponse(FollowChangeListBean response, final int id, int code) 
           {
                        
           }
           });

##Postgithub

  • 无需带参
OkHttpUtils.post()
               .url("xxxxx")
               .addHeader("Authorization", "xxxxx")
               .addParams("xxxxx", "xxxxx")
               .build()
               .execute(new GenericsCallback<xxxxx>(new JsonGenericsSerializator()) {
                    @Override
                    public void onError(Call call, Exception e, int id, int code) {
                       
                    }

                    @Override
                    public void onResponse(FollowChangeListBean.AddFollowListBean response, int id, int code) {
                       
                    }
                });
  • 需带参数
    好比:
{
   user_id:""
   guests:[{
     xxxx:"" 
    }]
 }
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
array.put(xxxxx);
try {
    object.put("user_id", xxxxx);
    object.put("guests",xxxxx);
    } 
    catch (JSONException e)
    {
     e.printStackTrace();
    }
    OkHttpUtils
             .postString()
             .url("xxxxx")
             .content(object.toString())
             .mediaType(MediaType.parse("application/json;charset=utf-8"))
             .build()
              .execute(new GenericsCallback<DeviceConfig>(new JsonGenericsSerializator()) {
              @Override
              public void onError(Call call, Exception e, int id, int code) 
              {

              }

              @Override
              public void onResponse(DeviceConfig response, int id, int code) 
              {
                       
              }
              });

##DELETEweb

  • 无需带参
OkHttpUtils.delete()
                .url("xxxxx")
                .addHeader("Authorization", "xxxxx")
                .build()
                .execute(new StringCallback() {
                 @Override
                 public void onError(Call call, Exception e, int id, int code) 
                 {
                        
                 }
                 @Override
                 public void onResponse(String response, int id, int code)
                 {
                 
                 }
                 });
  • 需带参数
JSONObject object = new JSONObject();
        try 
        {
          object.put("xxxxx", "xxxxx");
        } catch (JSONException e)
        {
          e.printStackTrace();
        }
OkHttpUtils.delete()
               .url("xxxxx")
               .addHeader("Authorization", "xxxxx")                    
               .requestBody(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), object.toString()))
               .build()
               .execute(new StringCallback() {
                 @Override
                public void onError(Call call, Exception e, int id, int code) 
                {
                         
                }
                @Override
                public void onResponse(String response, int id, int code) 
                {
                        
                }
                });

##PATCHjson

  • 无需带参
OkHttpUtils.patch()
              .url("xxxxx")
              .addHeader("Authorization","xxxxx")
              .build()
              .execute(new StringCallback() {
               @Override
               public void onError(Call call, Exception e, int id, int code) 
               {
                       
               }
              @Override
              public void onResponse(String response, int id, int code) 
              {
                
              }
              });
  • 需带参数
JSONObject object = new JSONObject();
try {
     object.put("xxxxx", "xxxxx");
     object.put("xxxxx",  "xxxxx");
    } catch (JSONException e) 
    {
      e.printStackTrace();
    }
 OkHttpUtils.patch()
              .url("xxxxx")
              .addHeader("Authorization","xxxxx")      
              .requestBody(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), object.toString()))
              .build()
              .execute(new StringCallback() {
 @Override
 public void onError(Call call, Exception e, int id, int code) 
         {
                        
          }

 @Override
 public void onResponse(String response, int id, int code) 
        {
                        
        }
        });