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
- Android-Retrofit初使用(二)
- Android-Retrofit初使用
- Retrofit使用教程(二)
- Retrofit使用教程(二)
- Retrofit使用教程(二)
- Android Retrofit 2.0(二)使用教程OkHttp3 + Gson + RxJava
- Android - Retrofit 基础使用
- Android Retrofit 2.0使用
- Android Retrofit使用详解
- Android Retrofit 2.0使用
- Android Retrofit使用记录
- android:retrofit+OKHttp使用
- android开发练习二-Retrofit
- android retrofit 示例(二)
- 【Android】Retrofit的使用(1)-Retrofit介绍
- Retrofit 简单使用之二
- Retrofit使用详解(二)
- Retrofit(二)、使用Retrofit+OkHttp下载文件并带进度条
- OpenLayers 3 之 地图矢量图层(ol.layer.Vector)详解,openlayersvector
- socket】深入浅出讲解:php的socket通信
- oc——类——instancetype
- JavaScript ES6/ES2015核心
- Weblogic 10.3.6.0 集群搭建
- Android-Retrofit初使用(二)
- CSS常见布局问题整理
- mysql Communications link failure Last packet sent to the server was X ms ago
- 使用CocoaPods的一些仓库说明
- 第二十四讲项目三 一元二次方程全解
- 跟着上手树莓派(一)—— 初次上手 & Mac 下刷入 Raspbian Jeesie 教程
- 深入PHP FTP类的详解
- AndRoid Notification
- linux之vim常见配置