Android-Retrofit初使用(二)

来源:互联网 发布:ecs windows 开启ftp 编辑:程序博客网 时间:2024/05/16 09:48

相信众多初学Retrofit的开发者都跟我一样,看到所谓的@Path,@Query,@QueryMap,@Field,@FieldMap,@Body 头都大了,这是什么?这是干嘛用的???接下来我将向大家一一说明

@Path:
作用:替换(填补),将Url中路径的某个文件地址,替换(填补)成你所传过来的地址本质:在基础Url(http://image.baidu.com/)上根据你所传入的参数aValue/cValue,填补成一个完整的Url:http://image.baidu.com/aValue/cValue      用这个完整Url发送了一次GET请求!
//自定义一个Interface接口类,用于存放所有的网络请求public interface RetrofitService{    @GET("category/{a}/{c}")     Call<ResponseBody> callPathBack(@Path("a") String aValue,@Path("c") String cValue);}
//创建Retrofit的实例Retrofit retrofit = new Retrofit.Builder().baseUrl("http://image.baidu.com/").build();
// 获取RetrofitService实例,发送请求RetrofitService service = retrofit.create(RetrofitService.class);Call<ResponseBody> call = service.callPathBack("image","dog");call.enqueue(new Callback<ResponseBody>() {...};
@Query:
本质:在基础Url(http://image.baidu.com/)上根据你所传入的参数category?a=image&c=dog,填补成一个完整的Url:http://image.baidu.com/category?a=image&c=dog      用这个完整Url发送了一次GET请求!
//自定义一个Interface接口类,用于存放所有的网络请求public interface RetrofitService{    @GET("category")    Call<ResponseBody> callQueryBack(@Query("a") String aValue,@Query("c") String cValue);}
// 创建Retrofit的实例Retrofit retrofit = new Retrofit.Builder().baseUrl("http://image.baidu.com/").build();
// 获取RetrofitService实例,发送请求RetrofitService service = retrofit.create(RetrofitService.class);Call<ResponseBody> call = service.callQueryBack("image","dog");call.enqueue(new Callback<ResponseBody>() {...};
@QueryMap:
本质:多个@Query。在基础Url(http://image.baidu.com/)上根据你所传入的参数category?a=image&c=dog,填补成一个完整的Url:http://image.baidu.com/category?a=image&c=dog      用这个完整Url发送了一次GET请求!
//自定义一个Interface接口类,用于存放所有的网络请求public interface RetrofitService{    @GET("category")    Call<ResponseBody> callQueryMapBack(@QueryMap Map<String,String> map);}
// 创建Retrofit的实例Retrofit retrofit = new Retrofit.Builder().baseUrl("http://image.baidu.com/").build();
// 获取RetrofitService实例,并发送请求RetrofitService service = retrofit.create(RetrofitService.class);         Map<String,String> map = new HashMap<>();         map.put("a","image");         map.put("c","dog");         Call<ResponseBody> call = service.callQueryMapBack(map);         call.enqueue(new Callback<ResponseBody>() {...};
@Field:
特点:1. 需要加@FormUrlEncoded 否则报错:@Field parameters can only be used with form encoding      2. FormUrlEncoded 修饰请求方法(只适用于POST请求)      3. 参数不会暴露
//自定义一个Interface接口类,用于存放所有的网络请求public interface RetrofitService{    //************ 固定参数写法 ************    @FormUrlEncoded    @POST("category")    Call<ResponseBody> callFieldBack(@Field("a") String aValue,@Field("c") String cValue);    //************ 不固定参数写法 ************     @FormUrlEncoded    @POST("category")    Call<ResponseBody> callFieldBack(@Field("a") String ...aValue);}
// 创建Retrofit的实例Retrofit retrofit = new Retrofit.Builder().baseUrl("http://image.baidu.com/").build();
//************ 固定参数写法 ************    RetrofitService service = retrofit.create(RetrofitService.class);    Call<ResponseBody> call = service.callFieldBack("image","dog");    call.enqueue(new Callback<ResponseBody>() {...};    本质:Url: http://image.baidu.com/category          Params: a=image&c=dog//************ 不固定参数写法 ************    RetrofitService service = retrofit.create(RetrofitService.class);    String[] params = {"image","adv","news"};    Call<ResponseBody> call = service.callFieldBack(params);    call.enqueue(new Callback<ResponseBody>() {...};    本质:Url: http://image.baidu.com/category          Params: a=image&a=adv&a=news
@FieldMap:
特点:相当于多个@Field,用法与@QueryMap一样,不再详述
@Body:
用法:定义一个模型用来存放参数键值对,将此模型传入服务器本质:Url: http://image.baidu.com/category      Params: a=image&c=dog
//自定义一个Interface接口类,用于存放所有的网络请求public interface RetrofitService{    /**    public class HttpParams {        String a;        String c;        public String getA() {            return a;        }        public void setA(String a) {            this.a = a;        }        public String getC() {            return c;        }        public void setC(String c) {            this.c = c;        }    }    */    @POST("category")    Call<ResponseBody> callBodyBack(@Body HttpParams params);}
/*** 创建Retrofit的实例* 与上面写法有一点点不同,需添加一个转换器* 转换器dependency:compile 'com.squareup.retrofit2:converter-gson:2.2.0'*/Retrofit retrofit = new Retrofit.Builder().baseUrl("http://image.baidu.com/").addConverterFactory(GsonConverterFactory.create()).build();
// 获取RetrofitService实例,发送请求RetrofitService service = retrofit.create(RetrofitService.class);HttpParams params = new HttpParams();params.setA("image");params.setC("dog");Call<ResponseBody> call = service.callBodyBack(params);call.enqueue(new Callback<ResponseBody>() {...};

附加:

//************ 自定义返回类型 ************//自定义一个Interface接口类,用于存放所有的网络请求public interface RetrofitService{    @GET("category")    Call<Entity> callCustomBack(@Query("a") String aValue,@Query("c") String cValue);    // 另外一种写法    @GET("category")    void callCustomBack(@Query("a") String aValue, @Query("c") String cValue, Callback<Entity> callback);}/** * 创建Retrofit的实例 * 注:此时Retrofit需要添加一个适配器 * 适配器dependency: compile 'com.squareup.retrofit2:adapter-rxjava:2.2.0' **/Retrofit retrofit = new Retrofit.Builder().baseUrl("http://image.baidu.com/").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
0 0
原创粉丝点击