Retrofit入门这一篇就够了

简介:大三学生党一枚!主攻Android开发,对于Web和后端均有了解。
个人语录取乎其上,得乎其中,取乎其中,得乎其下,以顶级态度写好一篇的博客。

一.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请求

get请求的请求参数是拼接在url中的,请看以下代码

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 = "http://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请求

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 = "http://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

传输数据类型JSON字符串,请看示例

    @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

Field已经介绍过了,就是传输键值对,有时候我们可能会需要传入多个键值对,此时使用FieldMap较好。

 @POST("/")
    @FormUrlEncoded
    Call<WeatherBeans> requestWeatherBeans(@FieldMap Map<String, String> fields);

关于注解,有一篇博客写的比较详细,建议阅读
Retrofit注解详解

三.总结

Retrofit是基于Okhttp的,但是又有自己的风格,一个很不错的网络请求框架!此外,我们使用Retrofit+RxJava进行开发也是事半功倍。

先别走,我有一个资源学习群要推荐给你,它是白嫖党的乐园,小白的天堂!
在这里插入图片描述
别再犹豫,一起来学习!
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_43927892/article/details/106327310