当前位置: 首页 > news >正文

展开说说Android之Retrofit详解_使用篇

Retrofit是由Square公司开发的类型安全HTTP客户端框架,借助动态代理运行时生成接口实现类将注解转化为OkHttp请求配置节省成本通过转换器(Gson/Moshi)自动序列化JSON/XML内部处理网络请求在主线程返回报文。Retrofit 直译是封装、翻版。他就是对okhttp做了进一步封装,方便使用,它底层的所有请求默认走的都是Okhttp。  所以使用Retrofit必须依赖okHttp。

两者区别:Retrofit是基于App发请求的封装,也就是面向的应用层(比如响应数据的处理和错误处理等),而Okhhtp是对底层网络请求的封装与优化(socket优化,数据压缩,buffer缓存等)

本文将通过GET/POSTde 基础请求、文件上传、动态URL、缓存转换器,以及借助OKHttp实现Retrofit请求过程中设置请求头、cookie、超时时间和拦截器都是借助OkHttpClient 等。

1、添加依赖:

使用Retrofit添加的依赖,另外,使用转换器也需要在build.gradle中单独引入,两种转换器通常不同时使用,根据项目JSON处理需求选择其一即可

dependencies {implementation 'com.squareup.retrofit2:retrofit:2.9.0'
}

GsonConverterFactory依赖配置

implementation 'com.squareup.retrofit2:converter-gson:2.9.0' 
implementation 'com.google.code.gson:gson:2.8.8' 

MoshiConverterFactory依赖配置: 

    implementation 'com.squareup.retrofit2:converter-moshi:2.9.0' 
    implementation 'com.squareup.moshi:moshi:1.14.0' // Moshi

      2、业务场景实现

      先定义一个接口:

      切记这里必须是interface!!!

      public interface RequestService {@GET("login/{id}")Call<LoginInfo> login(@Path("id") int userId);@POST("users/new")@Headers("Content-Type: application/json") // 设置请求头Call<ResponseBody> loginPost(@Body LoginInfo loginInfo); // @Body注解表示请求体@Multipart // 表示多部分请求@POST("upload")Call<ResponseBody> uploadFile( @Part("description") RequestBody description,  @Part MultipartBody.Part file);// 借助@Url注解,指定完整URL@GETCall<ResponseBody> getCustomUrl(@Url String url);//借助Query注解,添加查询参数@GET("search")Call<ResponseBody> queryData(@Query("q") String query);}

      定义实体类 LoginInfo.java

      public class LoginInfo {private String userNmae;private String userPwd;public LoginInfo(String userNmae, String userPwd) {this.userNmae = userNmae;this.userPwd = userPwd;}
      }

      定义初始化Retrofit的单例工具类 RetrofitUtil.java

      public class RetrofitUtil {private static RetrofitUtil retrofitUtil;private Retrofit retrofit;File httpCacheDirectory = new File(BaseApplication.getContext().getCacheDir(), "responses");int cacheSize = 10 * 1024 * 1024; // 10MBCache cache = new Cache(httpCacheDirectory, cacheSize);private RetrofitUtil(){}public static RetrofitUtil getInstance(){synchronized (RetrofitUtil.class){if (retrofitUtil == null){retrofitUtil = new RetrofitUtil();}return retrofitUtil;}}public <T> T getServer(Class<T> cls, String baseUrl){if (retrofit == null){retrofit = new Retrofit.Builder().baseUrl(baseUrl).client(getClient()).addConverterFactory(GsonConverterFactory.create()).build();}return retrofit.create(cls);}public OkHttpClient getClient(){// 创建OkHttpClientOkHttpClient okHttpClient = new OkHttpClient.Builder()
      //                .cookieJar().connectTimeout(10, TimeUnit.SECONDS).readTimeout(10, TimeUnit.SECONDS).writeTimeout(10, TimeUnit.SECONDS).addInterceptor(new Interceptor() {@Overridepublic Response intercept(Chain chain) throws IOException {Request request = chain.request();// 添加统一请求头request = request.newBuilder().header("Cache-Control", "public, max-age=60").build();return chain.proceed(request);}}).cache(cache).build();return okHttpClient;}
      }

      注意Retrofit在设置请求头、cookie、超时时间和拦截器都是借助OkHttpClient 来实现。

      2.1 Get请求

      getTv.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int id = 600001;RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).login(id).enqueue(new Callback<LoginInfo>() {@Overridepublic void onResponse(Call<LoginInfo> call, Response<LoginInfo> response) {if (response.isSuccessful()) {LoginInfo loginInfo = response.body(); // 自动解析JSON为User对象}}@Overridepublic void onFailure(Call<LoginInfo> call, Throwable t) {t.printStackTrace();}});}
      });

      2.2 POST请求

      LoginInfo loginInfo = new LoginInfo("Admin", "12345678");
      // 发起请求
      Call<ResponseBody> call = RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).loginPost(loginInfo);
      call.enqueue(new Callback<ResponseBody>() {@Overridepublic void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {// 请求成功}@Overridepublic void onFailure(Call<ResponseBody> call, Throwable t) {// 请求失败}
      });

      2.3 文件上传

      // 准备文件
      File file = new File("path/to/file.jpg");
      RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), file);
      MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);// 发起上传请求
      RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).uploadFile(RequestBody.create(MediaType.parse("text/plain"), "文件描述"),body).enqueue(new Callback<ResponseBody>() {@Overridepublic void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {//处理上传成功后的逻辑}@Overridepublic void onFailure(Call<ResponseBody> call, Throwable t) {//处理上传失败的逻辑}
      });

      2.4动态指定URL和添加查询参数

      一般来说一个项目中你可能定封装了公共个请求路径和域名,但有个别接口就需要特立独行的地址和传参,此时可以借助@Url和@Query动态的实现。 两个请求也在上面给出的RequestService 这个接口中定义了。

              // 使用动态URLCall<ResponseBody> call1 = RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).getCustomUrl("https://api.example.com/custom/path");// 使用查询参数Call<ResponseBody> call2 = RetrofitUtil.getInstance().getServer(RequestService.class,mUrl).queryData("retrofit");

      个人总结记录,才疏学浅,如有错误,欢迎指正,多谢。 

      相关文章:

    1. 深度解析MySQL中的Join算法:原理、实现与优化
    2. 如何在 Windows 上打造 macOS 式快速查看体验?
    3. 2025最新1000+道Java面试题+答案整理(附JVM/MySQL/Spring/Redis)
    4. 136.在 Vue3 中使用 OpenLayers 实现 GroupLayer 的添加、删除、显示、隐藏
    5. P2C (Point-to-Curve) 算法深度解析
    6. 传统的提示词优化中,损失函数有哪些
    7. iOS即时通讯发送图片消息内存暴涨优化
    8. 10.C S编程错误分析
    9. MySQL 自增主键 ID 设置为 0有坑
    10. 二叉树的最大深度题解
    11. Apache Kafka Connect任意文件读取漏洞(CVE-2025-27817)
    12. AWS Config:概述、优势以及如何开始?
    13. Jmeter的三种参数化方式详解
    14. 【WPF】WPF 中 `DisplayMemberPath` 与 `SelectedValuePath` 的深入理解与实战应用
    15. Kafka Connect架构深度解析:从分布式设计到组件交互机制
    16. 对抗串扰的第一武器
    17. 基于深度学习的智能视频内容理解系统:技术与实践
    18. 【JSON-To-Video】AI智能体开发:为视频图片元素添加动效(滑入、旋转、滑出),附代码
    19. 苍穹外卖--基于Spring Cache缓存套餐
    20. PSP专辑17本PDF
    21. 南宁哪里有做网站的公司/网游推广
    22. 自己做网站步骤 域名/人力资源短期培训班
    23. 代做财务报表分析网站/seo搜索优化公司报价
    24. 江都住房和建设局网站/长尾关键词搜索网站
    25. 法律类微信网站建设/广州网站建设推广专家
    26. 在网站制作意见征集是怎么做的/免费建一个自己的网站