Retrofit工具类的封装

本文介绍了一个使用Retrofit结合OkHttp进行网络请求封装的实用案例。该案例通过单例模式创建Retrofit实例,并配置了OkHttp客户端实现网络请求的日志记录、超时设置及缓存管理等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class RetrofitUtils {
    private static RetrofitUtils retrofitUtils=null;
    private OkHttpClient okHttpClient;

    public static RetrofitUtils newInstance(){
        if(retrofitUtils==null){
            synchronized(RetrofitUtils.class){
                if(retrofitUtils==null){
                    retrofitUtils=new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }

    //构造方法 添加okhttpclient
    public RetrofitUtils(){
        //缓存目录
        File sdcache = new File(Environment.getExternalStorageDirectory(), "cache");
        int cacheSize = 10 * 1024 * 1024;
        //OkHttp3拦截器
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("xxx", message.toString());
            }
        });
        //Okhttp3的拦截器日志分类 4种
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);


        okHttpClient = new OkHttpClient.Builder().connectTimeout(15, TimeUnit.SECONDS)
                //添加OkHttp3的拦截器
                .addInterceptor(httpLoggingInterceptor)
                .writeTimeout(20, TimeUnit.SECONDS).readTimeout(20, TimeUnit.SECONDS)
                .cache(new Cache(sdcache.getAbsoluteFile(), cacheSize))
                .build();

    }
    //创建retrofitservice类
    public RetrofitService create(Class<RetrofitService> retrofitService, String baseurl){
        Retrofit retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(baseurl)
                .client(okHttpClient)
                .build();
        RetrofitService service = retrofit.create(retrofitService);
        return service;
    }

}
### 华为 OD 真题文档获取方式 对于准备参加华为 OD 的技术人员来说,获取高质量的真题资料是非常重要的。以下是关于如何下载或查看华为 OD 真题的相关信息。 #### 文档下载地址 可以通过以下链接访问并下载一份名为“华为OD真题.pdf”的资源文件[^1]: - **项目地址**: [https://gitcode.com/Open-source-documentation-tutorial/ee1fb](https://gitcode.com/Open-source-documentation-tutorial/ee1fb) 此仓库提供了华为 OD 相关的题集合,适合用于备考和技术练习。 #### 题目解析语言支持 除了直接下载真题外,还可以通过一些博客文章获得针对不同编程语言的具体题解和指导。这些资源涵盖了多种主流开发语言,包括但不限于 Python、C++、Java 和 JavaScript 等[^2]: - **Python** 解析: [华为 OD 考 Python](https://blog.youkuaiyun.com/hihell/category_12199275.html) - **C++** 解析: [华为 OD 考 C++](https://blog.youkuaiyun.com/hihell/category_12199283.html) - **C 语言** 解析: [华为 OD 考 True C](https://blog.youkuaiyun.com/hihell/category_12225286.html) - **Java** 解析: [华为 OD 考 Java](https://blog.youkuaiyun.com/hihell/category_12201821.html) - **JavaScript** 解析: [华为 OD 考 Js](https://blog.youkuaiyun.com/hihell/category_12201825.html) - **Golang** 解析: [华为 OD Golang](https://blog.youkuaiyun.com/hihell/category_12231589.html) 以上链接不仅包含了具体的题目描述,还附带了解决方案以及代码实现示例[^3]。 #### 输入验证的重要性 值得注意的是,在实际编写解决方案时应特别关注输入数据的有效性和合法性检查。例如,当处理用户数量 `n` 的情况下,需确认其是否为正整数;而对于其他参数,则要确保它们均为有效数值类型[^4]。这一步骤能够显著提升程序运行稳定性及准确性。 ```python def validate_input(n, values): """ Validate the input parameters. Args: n (int): Number of users. values (list[int]): List of system consumption values. Returns: bool: Whether all inputs are valid or not. """ if isinstance(n, int) and n > 0 and all(isinstance(x, int) for x in values): return True return False ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值