1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| 🔧 一、依赖配置(build.gradle)
// Retrofit 核心 implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
// OkHttp3 + 日志拦截器 implementation 'com.squareup.okhttp3:okhttp:4.12.0' implementation 'com.squareup.okhttp3:logging-interceptor:4.12.0'
// RxJava2 + Retrofit 支持 implementation 'io.reactivex.rxjava2:rxjava:2.2.21' implementation 'io.reactivex.rxjava2:rxandroid:2.1.1' implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
// MVVM 架构核心 implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.lifecycle:lifecycle-viewmodel:2.6.2' 🧱 二、Retrofit 初始化封装(ApiClient.java)
import android.util.Log;
import okhttp3.OkHttpClient; import okhttp3.logging.HttpLoggingInterceptor; import retrofit2.Retrofit; import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory; import retrofit2.converter.gson.GsonConverterFactory;
/** * @author xqm * @date 2025/7/12 16:20 * @description https://jsonplaceholder.typicode.com/ * retrofit+okhttp */ public class ApiClient { private static final String BASE_URL = "https://xxxxx/"; private static Retrofit retrofit;
public static Retrofit getInstance() { if (retrofit == null) { // 添加日志拦截器 HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); // HttpLoggingInterceptor logging = new HttpLoggingInterceptor( // message -> Log.e("xqm", message) // 你可以自定义日志输出位置 // ); logging.setLevel(HttpLoggingInterceptor.Level.BODY);
logging.setLevel(HttpLoggingInterceptor.Level.BODY); // 打印完整请求和响应
OkHttpClient client = new OkHttpClient.Builder() .addInterceptor(new HeaderInterceptor()) .addInterceptor(logging) .build();
retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .client(client) .addConverterFactory(GsonConverterFactory.create()) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // Rx 支持 .build(); } return retrofit; } }
🔐 三、自定义请求头拦截器(HeaderInterceptor.java)
import java.io.IOException;
import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response;
/** * @author xqm * @date 2025/7/12 16:21 * @description 用于自动在所有请求中附加 token 认证信息,可在登录成功后设置。 */ public class HeaderInterceptor implements Interceptor { private static String token = "";
public static void setToken(String t) { token = t; }
@Override public Response intercept(Interceptor.Chain chain) throws IOException { Request request = chain.request(); Request.Builder builder = request.newBuilder() .addHeader("Content-Type", "application/json") .addHeader("Authorization", token.isEmpty() ? "" : "Bearer " + token);
return chain.proceed(builder.build()); } }
🔌 四、API 接口定义(ApiService.java)
import io.reactivex.Observable; import retrofit2.http.Body; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query;
/** * @author xqm * @date 2025/7/12 16:20 * @description ApiService 接口定义 * 支持 GET 和 POST 请求 * 支持 @Query 参数和 @Body 参数 * 使用 RxJava 返回 Observable<T> */ public interface ApiService {
@GET("todos/1") Observable<UserModel> getUserInfo();
@GET("access/auth/login") Observable<UserModel> getlogin(@Query("username") String username, @Query("password") String password);
@POST("user/login") Observable<LoginResponse> login(@Body LoginRequest request); }
📦 五、数据访问封装(UserRepository.java)
import io.reactivex.Observable;
/** * @author xqm * @date 2025/7/12 16:21 * @description 将数据访问封装在 Repository 层,如网络请求,本地数据库访问等 */ public class UserRepository {
private final ApiService api;
public UserRepository() { api = ApiClient.getInstance().create(ApiService.class); }
public Observable<LoginResponse> login(String username, String password) { return api.login(new LoginRequest(username, password)); }
public Observable<UserModel> getUserInfo() { return api.getUserInfo(); }
public Observable<UserModel> getLogin(String userName,String pwd) { return api.getlogin(userName,pwd); } }
🧠 六、ViewModel 逻辑层(UserViewModel.java)
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.disposables.Disposable; import io.reactivex.schedulers.Schedulers;
/** * @author xqm * @date 2025/7/12 16:22 * @description 调用 Repository 提供的数据接口,处理线程调度和生命周期管理。 */ public class UserViewModel extends BaseViewModel { private final UserRepository repository = new UserRepository();
public void login(String username, String password, RxObserver<LoginResponse> observer) { Disposable disposable = repository.login(username, password) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(observer); addDisposable(disposable); }
public void getUserInfo(RxObserver<UserModel> observer) { Disposable disposable = repository.getUserInfo() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(observer); addDisposable(disposable); }
public void getLogin(String userName,String pwd,RxObserver<UserModel> observer) { Disposable disposable = repository.getLogin(userName,pwd) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribeWith(observer); addDisposable(disposable); } }
🧼 七、BaseViewModel(统一管理 Disposable)
/** * @author xqm * @date 2025/7/12 16:22 * @description 避免内存泄漏,销毁 ViewModel 时自动清理订阅。 */ public abstract class BaseViewModel extends ViewModel { protected CompositeDisposable compositeDisposable = new CompositeDisposable();
protected void addDisposable(Disposable d) { compositeDisposable.add(d); }
@Override protected void onCleared() { compositeDisposable.clear(); super.onCleared(); } }
🧪 八、自定义 Observer 示例(RxObserver)
/** * @author xqm * @date 2025/7/12 16:43 * @description 可自我管理订阅生命周期的 Observer,既能收数据,又能自己取消订阅 */ import io.reactivex.observers.DisposableObserver;
public abstract class RxObserver<T> extends DisposableObserver<T> { @Override public void onNext(T t) { onSuccess(t); }
@Override public void onError(Throwable e) { onFailure(e); }
@Override public void onComplete() {}
public void onStart() {}
public abstract void onSuccess(T t); public abstract void onFailure(Throwable e); }
可以在回调里更新 UI,比如: viewModel = new ViewModelProvider(this).get(UserViewModel.class); viewModel.login(user, pwd, new RxObserver<LoginResponse>() { @Override public void onNext(LoginResponse response) { if (response.getCode() == 200) { // 登录成功 HeaderInterceptor.setToken(response.getToken()); Toast.makeText(context, "登录成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(context, "登录失败:" + response.getMsg(), Toast.LENGTH_SHORT).show(); } }
@Override public void onError(Throwable e) { Toast.makeText(context, "请求出错: " + e.getMessage(), Toast.LENGTH_SHORT).show(); } });
|