简介
Retrofit 是一个功能强大的网络请求库,能够简化 Android 应用中网络请求的使用。本文介绍了一个封装了 Retrofit 的工具类:RetrofitUtils。该工具类支持 GET 和 POST 请求,并且还提供了图片上传的功能。
RetrofitUtils 的使用
首先,在项目的 build.gradle
文件中添加以下依赖项:
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:4.9.1'
接下来,创建一个 RetrofitUtils
类,该类封装了 Retrofit 相关的方法。下面是该类的代码:
public class RetrofitUtils {
private static final String BASE_URL = "http://api.example.com/";
private static Retrofit retrofit;
private static void initRetrofit() {
if (retrofit == null) {
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BODY))
.build();
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
}
public static void doGet(String url, Callback<ResponseBody> callback) {
initRetrofit();
ApiService apiService = retrofit.create(ApiService.class);
Call<ResponseBody> call = apiService.doGet(url);
call.enqueue(callback);
}
public static void doPost(String url, Map<String, Object> params, Callback<ResponseBody> callback) {
initRetrofit();
ApiService apiService = retrofit.create(ApiService.class);
Call<ResponseBody> call = apiService.doPost(url, params);
call.enqueue(callback);
}
public static void uploadImage(String url, File imageFile, Callback<ResponseBody> callback) {
initRetrofit();
ApiService apiService = retrofit.create(ApiService.class);
RequestBody requestBody = RequestBody.create(MediaType.parse("image/*"), imageFile);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("image", imageFile.getName(), requestBody);
Call<ResponseBody> call = apiService.uploadImage(url, filePart);
call.enqueue(callback);
}
}
在上述代码中,我们通过调用 Retrofit 的 enqueue()
方法来异步执行网络请求。同时,我们还创建了一个 ApiService
接口,用于定义请求的接口,其代码如下:
public interface ApiService {
@GET
Call<ResponseBody> doGet(@Url String url);
@FormUrlEncoded
@POST
Call<ResponseBody> doPost(@Url String url, @FieldMap Map<String, Object> params);
@Multipart
@POST
Call<ResponseBody> uploadImage(@Url String url, @Part MultipartBody.Part file);
}
如上所示,我们通过注解来指定请求的类型以及相应的参数。同时,RetrofitUtils 类中的 doGet()
、doPost()
和 uploadImage()
方法也是根据这些注解来创建相应的请求。
使用示例
发起 GET 请求
RetrofitUtils.doGet("https://api.example.com/data", new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// 处理响应
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
// 处理错误
}
});
发起 POST 请求
Map<String, Object> params = new HashMap<>();
params.put("name", "example");
RetrofitUtils.doPost("https://api.example.com/data", params, new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// 处理响应
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
// 处理错误
}
});
上传图片
File imageFile = new File("path/to/image.jpg");
RetrofitUtils.uploadImage("https://api.example.com/upload", imageFile, new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// 处理响应
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable throwable) {
// 处理错误
}
});
总结
使用 RetrofitUtils 可以方便地进行网络请求,包括 GET 和 POST 请求以及图片上传等。该工具类封装了 Retrofit 相关的方法,大大简化了网络请求的处理过程。同时,通过定义 ApiService 接口,我们可以统一管理和维护网络请求的接口,使代码更加清晰易读。
希望本文对你有所帮助,欢迎提出宝贵意见和建议。
本文来自极简博客,作者:时光倒流,转载请注明原文链接:Android RetrofitUtils 工具类:支持 GET 和 POST 请求、支持图片上传