Android 帮我搭建一个MVP框架,语言使用Java,包含Okhttp+Retrofit网络封装,Base基类的抽取,APPLocation的代码
时间: 2024-04-01 07:32:01 浏览: 139
好的,以下是一个简单的 MVP 框架,包含 Okhttp+Retrofit 网络封装,Base 基类的抽取以及 APPLocation 的代码:
- 首先创建一个 BaseView 接口,定义一些公共的 UI 操作方法:
public interface BaseView {
void showLoading();
void hideLoading();
void showError(String error);
}
- 接着创建一个 BasePresenter 类,定义一些公共的 Presenter 操作方法:
public class BasePresenter<V extends BaseView> {
private WeakReference<V> mViewRef;
public void attachView(V view) {
mViewRef = new WeakReference<>(view);
}
public void detachView() {
if (mViewRef != null) {
mViewRef.clear();
mViewRef = null;
}
}
public boolean isViewAttached() {
return mViewRef != null && mViewRef.get() != null;
}
public V getView() {
return mViewRef.get();
}
public void checkViewAttached() {
if (!isViewAttached()) throw new RuntimeException("Please call attachView() before requesting data to the Presenter");
}
}
- 创建一个 BaseActivity 类,作为所有 Activity 的基类,包含一些公共的操作:
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity implements BaseView {
protected P mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
mPresenter = createPresenter();
if (mPresenter != null) {
mPresenter.attachView(this);
}
initView();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
}
protected abstract int getLayoutId();
protected abstract P createPresenter();
protected abstract void initView();
}
- 接着创建一个 BaseFragment 类,作为所有 Fragment 的基类,也包含一些公共的操作:
public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements BaseView {
protected P mPresenter;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = createPresenter();
if (mPresenter != null) {
mPresenter.attachView(this);
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (mPresenter != null) {
mPresenter.detachView();
}
}
protected abstract P createPresenter();
}
- 创建一个 AppApplication 类,作为整个应用程序的入口,包含一些公共的配置信息和初始化操作:
public class AppApplication extends Application {
private static AppApplication sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
// 初始化网络请求
RetrofitClient.getInstance().init(this);
}
public static AppApplication getInstance() {
return sInstance;
}
}
- 创建一个 RetrofitClient 类,用于封装 Okhttp+Retrofit 网络请求:
public class RetrofitClient {
private static final String TAG = "RetrofitClient";
private static final int DEFAULT_TIMEOUT = 30;
private Retrofit mRetrofit = null;
private OkHttpClient mOkHttpClient = null;
private RetrofitClient() {}
public static RetrofitClient getInstance() {
return SingletonHolder.INSTANCE;
}
private static class SingletonHolder {
private static final RetrofitClient INSTANCE = new RetrofitClient();
}
public void init(Context context) {
HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
mOkHttpClient = new OkHttpClient.Builder()
.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.writeTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS)
.addInterceptor(loggingInterceptor)
.addInterceptor(new TokenInterceptor())
.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(ApiService.BASE_URL)
.client(mOkHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}
public ApiService getApiService() {
return mRetrofit.create(ApiService.class);
}
}
- 创建一个 ApiService 接口,定义所有的网络请求接口:
public interface ApiService {
String BASE_URL = "https://www.example.com/";
@POST("login")
Observable<BaseResponse<User>> login(@Query("username") String username, @Query("password") String password);
}
- 最后,我们可以创建一个 LoginPresenter 类,来处理登录相关的业务逻辑:
public class LoginPresenter extends BasePresenter<LoginContract.View> implements LoginContract.Presenter {
private ApiService mApiService;
public LoginPresenter() {
mApiService = RetrofitClient.getInstance().getApiService();
}
@Override
public void login(String username, String password) {
checkViewAttached();
getView().showLoading();
mApiService.login(username, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<BaseResponse<User>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(BaseResponse<User> userBaseResponse) {
getView().hideLoading();
if (userBaseResponse.getCode() == 0) {
getView().loginSuccess(userBaseResponse.getData());
} else {
getView().showError(userBaseResponse.getMsg());
}
}
@Override
public void onError(Throwable e) {
getView().hideLoading();
getView().showError(e.getMessage());
}
@Override
public void onComplete() {
}
});
}
}
以上就是一个简单的 MVP 框架的实现,您可以根据自己的需求进行修改和扩展。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)