package com.example.myapplication.mvvm.http; import java.util.concurrent.TimeUnit; import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory; public class RetrofitManager { public RetrofitManager() { } private static RetrofitManager retrofitManager; public static RetrofitManager getInstance() { if (retrofitManager == null) { retrofitManager = new RetrofitManager(); } return retrofitManager; } private Retrofit retrofit; public Retrofit createRetrofit() { OkHttpClient ok = new OkHttpClient.Builder() .writeTimeout(5, TimeUnit.MINUTES) .connectTimeout(5, TimeUnit.MINUTES) .readTimeout(5, TimeUnit.MINUTES) .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)) .build(); if (retrofit == null) { retrofit = new Retrofit.Builder() .client(ok) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl("http://10.161.9.80:7012/") .build(); } return retrofit; } }
mvvm/RetrofitManager
最新推荐文章于 2024-11-04 11:45:54 发布
本文介绍了在Android应用中使用Retrofit库创建一个高效的网络请求管理器,包括OkHttp配置、超时处理以及数据解析适配器的选择,适用于MVVM架构。
摘要由CSDN通过智能技术生成