除了 android 中的 activity 之外,调用网络 class 的好方法是什么?
What can be a good way to call network class other than an activity in android?
我创建了一个网络客户端应用程序(使用 Retrofit),我在 activity 中调用网络请求和响应。我了解到这是一种不好的做法。如果有人可以建议我可以遵循什么好的设计模式来进行这样的操作?
对于开始,如果您必须从头开始创建应用程序,可能会尝试遵循一种架构,因为您正在寻找网络调用,
您可以将 MVVM 用于最佳实践,它还以最佳方式处理 api 请求 正如您从图中看到的那样,
这种架构,基本上将视图(UI)与视图模型(视图逻辑)
分开
这取决于你想如何开发应用程序,这意味着你可以跳过存储库并处理视图模型中的网络调用,或者你可以创建一个存储库 class 并将所有网络相关的东西即:网络调用和类似的东西。
参考教程:https://learntodroid.com/consuming-a-rest-api-using-retrofit2-with-the-mvvm-pattern-in-android/
像这样创建一个 RetrofitClient Class
public class RetrofitClient {
public static final String BASE_URL = "YOUR BASE URL HERE";
public static RetrofitClient mInstance;
private Retrofit retrofit;
public static RetrofitClient getInstance() {
if (mInstance == null)
mInstance = new RetrofitClient();
return mInstance;
}
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public ApiInterface getApiInterface() {
return retrofit.create(ApiInterface.class);
}
public interface ApiInterface {
@GET("api_name_here")
Call<ResponseBody> methodName();
}
}
存储库class
public class MyRepository {
private static MyRepository mInstance;
public static MyRepository getInstance() {
if (mInstance == null)
mInstance = new MyRepository();
return mInstance;
}
private MyRepository(){
}
private LiveData<T> getData(){
MutableLiveData<T> liveData = new MutableLiveData<>();
RetrofitClient.getInstance()
.getApiInterface()
.methodName()
.enqueue(new Callback<T>() {
@Override
public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
liveData.setValue(response.body());
}
@Override
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
// handleFailure
}
});
return liveData;
}
}
视图模型Class
public class MyViewModel extends ViewModel{
private MyRepository myRepository;
public MyViewModel(){
myRepository = MyRepository.getInstance();
}
public LiveData<T> getData(){
return myRepository.getData();
}
}
然后在你的Activity或片段
MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
myViewModel.getData().observe(this, new Observer<T>() {
@Override
public void onChanged(T t) {
// handle your data here...
}
});
我创建了一个网络客户端应用程序(使用 Retrofit),我在 activity 中调用网络请求和响应。我了解到这是一种不好的做法。如果有人可以建议我可以遵循什么好的设计模式来进行这样的操作?
对于开始,如果您必须从头开始创建应用程序,可能会尝试遵循一种架构,因为您正在寻找网络调用,
您可以将 MVVM 用于最佳实践,它还以最佳方式处理 api 请求 正如您从图中看到的那样, 这种架构,基本上将视图(UI)与视图模型(视图逻辑)
分开这取决于你想如何开发应用程序,这意味着你可以跳过存储库并处理视图模型中的网络调用,或者你可以创建一个存储库 class 并将所有网络相关的东西即:网络调用和类似的东西。
参考教程:https://learntodroid.com/consuming-a-rest-api-using-retrofit2-with-the-mvvm-pattern-in-android/
像这样创建一个 RetrofitClient Class
public class RetrofitClient {
public static final String BASE_URL = "YOUR BASE URL HERE";
public static RetrofitClient mInstance;
private Retrofit retrofit;
public static RetrofitClient getInstance() {
if (mInstance == null)
mInstance = new RetrofitClient();
return mInstance;
}
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public ApiInterface getApiInterface() {
return retrofit.create(ApiInterface.class);
}
public interface ApiInterface {
@GET("api_name_here")
Call<ResponseBody> methodName();
}
}
存储库class
public class MyRepository {
private static MyRepository mInstance;
public static MyRepository getInstance() {
if (mInstance == null)
mInstance = new MyRepository();
return mInstance;
}
private MyRepository(){
}
private LiveData<T> getData(){
MutableLiveData<T> liveData = new MutableLiveData<>();
RetrofitClient.getInstance()
.getApiInterface()
.methodName()
.enqueue(new Callback<T>() {
@Override
public void onResponse(@NonNull Call<T> call, @NonNull Response<T> response) {
liveData.setValue(response.body());
}
@Override
public void onFailure(@NonNull Call<T> call, @NonNull Throwable t) {
// handleFailure
}
});
return liveData;
}
}
视图模型Class
public class MyViewModel extends ViewModel{
private MyRepository myRepository;
public MyViewModel(){
myRepository = MyRepository.getInstance();
}
public LiveData<T> getData(){
return myRepository.getData();
}
}
然后在你的Activity或片段
MyViewModel myViewModel = new ViewModelProvider(this).get(MyViewModel.class);
myViewModel.getData().observe(this, new Observer<T>() {
@Override
public void onChanged(T t) {
// handle your data here...
}
});