简介:大三学生党一枚!主攻Android开发,对于Web和后端均有了解。 使用前需要导入依赖 get请求的请求参数是拼接在url中的,请看以下代码 其中 解释一下上面 看一下我们如何在代码中调用这个接口! POST请求一般用来提交表单,修改数据库中的表内容。它的字段是包含在请求体中的。 调用登录请求的过程和 用于修饰 在 动态方式:作为参数,从调用者传入 传输数据类型JSON字符串,请看示例 用 动态配置 这样我们就可在调用的地方传入 Field已经介绍过了,就是传输键值对,有时候我们可能会需要传入多个键值对,此时使用FieldMap较好。 关于注解,有一篇博客写的比较详细,建议阅读 先别走,我有一个资源学习群要推荐给你,它是白嫖党的乐园,小白的天堂!
个人语录:取乎其上,得乎其中,取乎其中,得乎其下,以顶级态度写好一篇的博客。
Retrofit入门
一.Retrofit介绍
Retrofit
是一款针对Android
网络请求的框架,它的底层是基于Okhttp
实现的,它的特点是使用运行时的注解提供功能。 //retrofit implementation 'com.squareup.retrofit2:retrofit:2.1.0' //Gson converter implementation 'com.squareup.retrofit2:converter-gson:2.1.0' //RxJava2 Adapter implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' //okhttp implementation 'com.squareup.okhttp3:okhttp:3.4.1' implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
Retrofit
的重点还在于它的注解。二.Retrofit注解
Retrofit
注解种类多,只要把注解掌握,那么Retrofit
的使用就易如反掌。2.1 请求方法注解
2.1.1 GET请求
public interface LoginService { @GET("userAccount=1001") Call<Result> findUserPwd();//GET方法根据userAccount查询密码的接口方法 }
Result
是我们自定义的实体类,它包含了我们期望服务器返回的一些字段,在服务端,对于登录请求,我定义返回了三个字段。package com.ibuyi.android.rxjava; public class Result { private String res;//登录结果 private String msg;//附加信息 private int type;//用户类别 public String getRes() { return res; } public void setRes(String res) { this.res = res; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public int getType() { return type; } public void setType(int type) { this.type = type; } }
LoginService
中的findUserPwd
方法,这是一个GET请求,并且指明了userAccount=1001,它的返回值是一个Call对象,我们通过Call对象执行操作。public void findUserPwd(){ String baseUrl = "https://xxxxx:8080/user/"; //注意,baseUrl必须以“/”结尾 Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl)//绑定基础的ip信息等 //最后我们访问的完整的url是 //baseUrl+GET请求中的键值对 //addconvertFactory添加对JSON返回值的支持 .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); //通过retrofit.create方法创建我们的接口 LoginService login = retrofit.create(LoginService.class); //调用我们的findUserPwd方法返回一个Call请求,并且进行异步执行 Call<Result> call = login.findUserPwd(); call.enqueue(new Callback<Result>() { @Override public void onResponse(Call<Result> call, Response<Result> response) { } @Override public void onFailure(Call<Result> call, Throwable t) { } }); }
2.1.2 POST请求
public interface LoginService { //FormUrlEncoded表明这是一个表单请求 @FormUrlEncoded @POST("login")//"login是我们要访问的服务,完整的url是baseUrl+login" Observable<Result> userLogin(@Field("userAccount") String userAccount, @Field("userPwd") String userPwd); //@Field注解是我们要携带键值对的键的名称,在服务端,我们可以通过该键取出传入的值 //Observable是使用了RxJava,如果不熟悉RxJava可以改为Call,或者查看我写的RxJava小白的入门教程,非常简单。但RxJava非常非常非常强大! }
findUserPwd
几乎一样.不同的是我们需要传入用户名和密码。
小白可以模仿GET
请求,自己动手创建一个POST
登录请求。2.2 标记类注解
2.2.1 FormUrlEncoded
Field
注解和FieldMap
注解
使用该注解,表示请求正文将使用表单网址编码。字段应该声明为参数,并用@Field
注释或FieldMap
注释。一言以蔽之,使用POST
请求时,一般都会加上@FormUrlEncoded
,特别是有参数时。2.2.2 Multipart
Multipart
注解表示允许多个@Part
注解,其中@Part
注解是用来上传文件的,如图片,视频等。下面是用Retrofit
上传图片的代码。public interface UpPhoto { @Multipart @POST("photo")//photo表示路径 Call<Result> upPhoto(@Part MultipartBody.Part photo, @Part("description")RequestBody description); //@Part注解和@Field注解非常类似,只不过Field是用来传输一些基本数据类型,@Part可以用来传入文件 //大体上就是声明类型不同,实质作用都是注解传入的参数 }
public void upPic() throws URISyntaxException { String baseUrl = "https://10.138.51.142:8080/user/"; Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .build(); URI uri = new URI("/sdcard/Pictures/pic/photo.png"); File file = new File(uri); RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/png"),file); //photiRequestBody是用来描述与上传图片相关信息的载体,如类型,文件等,用来创建MultipartBody.Part MultipartBody.Part photo = MultipartBody.Part.createFormData("photos", "photo.png", photoRequestBody); UpPhoto up = retrofit.create(UpPhoto.class); RequestBody description = RequestBody.create(null, "hello,the pic is uping!"); //desciption用来携带描述信息 Call<Result> call = up.upPhoto(photo, description); call.enqueue(new Callback<Result>() { @Override public void onResponse(Call<Result> call, Response<Result> response) { } @Override public void onFailure(Call<Result> call, Throwable t) { } }); }
2.2.3 Streaming
Streming
代表相应的数据以流的形式返回,如果不使用他,则默认会把全部数据加载到内存,所以在下载大文件时需要加上折耳根注解。2.3 参数类注解
2.3.1 Header和Headers
http
请求中为了防止攻击或者过滤掉不安全的访问,或者添加特殊加密的访问等,以便减轻服务器的压力保证请求安全,通常都会在消息报头中携带一些特殊的消息头处理。
使用@Header
添加消息报头,有静态方式和动态方式。
静态方式:直接写死public interface UpPhoto { @Multipart @POST("photo")//photo表示路径 @Headers("Accept-Encoding:application/json")//静态方式写死 Call<Result> upPhoto(@Part MultipartBody.Part photo, @Part("description")RequestBody description); }
public interface UpPhoto { @Multipart @POST("photo")//photo表示路径 Call<Result> upPhoto(@Part MultipartBody.Part photo, //作为参数传入 @Part("description")RequestBody description,@Header("Location") String location); }
2.3.2 Body
@POST("login") Call<Result> userLogin(@Body User user);
@Body
这个注解会自动把User
转换成json
字符串。2.3.3 Path
URL
地址 @POST("{path}") Call<Result> userLogin(@Path("path") String path);
Url
了。2.3.4 Field和FieldMap
@POST("/") @FormUrlEncoded Call<WeatherBeans> requestWeatherBeans(@FieldMap Map<String, String> fields);
Retrofit注解详解三.总结
Retrofit
是基于Okhttp
的,但是又有自己的风格,一个很不错的网络请求框架!此外,我们使用Retrofit+RxJava
进行开发也是事半功倍。
别再犹豫,一起来学习!
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算