okhttp-utils使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sunmmer123/article/details/78498018

okhttp-utils使用

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


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) 
           {

           }
           });

Post

  • 无需带参
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) 
              {

              }
              });

DELETE

  • 无需带参
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) 
                {

                }
                });

PATCH

  • 无需带参
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) 
        {

        }
        });

猜你喜欢

转载自blog.csdn.net/sunmmer123/article/details/78498018