Android 帮我搭建一个MVP框架,语言使用Java,包含Okhttp+Retrofit网络封装,Base基类的抽取,APPLocation的代码并且能直接使用写项目的那种,包含包名结构
时间: 2024-03-24 11:37:42 浏览: 17
好的,我可以帮助你搭建一个基于MVP模式的Android项目框架,其中包含Okhttp+Retrofit网络封装、Base基类的抽取、APPLocation的代码,且能直接使用写项目的那种,并包含包名结构。
首先,我们需要创建一个Android项目,然后按照以下步骤进行框架搭建:
1. 创建包名结构
可以按照以下结构创建包名:
- app:存放应用程序的 Activities 和 Application 类
- base:存放 Base 类
- model:存放数据模型相关的类
- presenter:存放 Presenter 类
- view:存放 View 类和相关接口
- utils:存放工具类
- net:存放网络请求相关的类
2. 添加依赖
在 app 模块的 build.gradle 文件中添加以下依赖:
```
dependencies {
// Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
// OkHttp
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
// ButterKnife
implementation 'com.jakewharton:butterknife:10.2.3'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
// Gson
implementation 'com.google.code.gson:gson:2.8.7'
// RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.2.19'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
}
```
3. 创建 BaseActivity 类
BaseActivity 类是所有 Activity 的基类,它包含一些通用的方法和属性,如 Toast 弹窗、ProgressDialog 等。
```
public abstract class BaseActivity extends AppCompatActivity {
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
ButterKnife.bind(this);
initView();
initData();
}
protected abstract int getLayoutId();
protected abstract void initView();
protected abstract void initData();
protected void showToast(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
protected void showProgressDialog() {
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("加载中...");
mProgressDialog.setCancelable(false);
}
if (!mProgressDialog.isShowing()) {
mProgressDialog.show();
}
}
protected void hideProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
```
4. 创建 BasePresenter 类
BasePresenter 类是所有 Presenter 的基类,它包含一些通用的方法和属性,如 attachView()、detachView() 等。
```
public abstract class BasePresenter<V extends BaseView> {
private V mView;
public void attachView(V view) {
mView = view;
}
public void detachView() {
mView = null;
}
protected V getView() {
return mView;
}
protected boolean isViewAttached() {
return mView != null;
}
}
```
5. 创建 BaseView 接口
BaseView 接口是所有 View 的基类接口,它包含一些通用的方法和属性,如 showLoading()、hideLoading() 等。
```
public interface BaseView {
void showLoading();
void hideLoading();
void showError(String errorMsg);
}
```
6. 创建 AppApplication 类
AppApplication 类是整个应用程序的入口点,它继承自 Application 类。在该类中可以初始化一些全局的变量、配置等。
```
public class AppApplication extends Application {
private static AppApplication sInstance;
@Override
public void onCreate() {
super.onCreate();
sInstance = this;
}
public static AppApplication getInstance() {
return sInstance;
}
}
```
7. 创建 RetrofitUtils 类
RetrofitUtils 类是用于封装 Retrofit 和 OkHttp 的网络请求封装类,它包含一些通用的方法和属性,如 create()、get()、post() 等。
```
public class RetrofitUtils {
private static Retrofit sRetrofit;
public static Retrofit create(String baseUrl) {
if (sRetrofit == null) {
synchronized (RetrofitUtils.class) {
if (sRetrofit == null) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
sRetrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
}
}
}
return sRetrofit;
}
public static <T> void get(String url, Map<String, String> params, final HttpCallback<T> callback) {
Retrofit retrofit = create(url);
ApiService apiService = retrofit.create(ApiService.class);
Call<ResponseBody> call = apiService.get(url, params);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String result = response.body().string();
T data = new Gson().fromJson(result, callback.getType());
callback.onSuccess(data);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
public static <T> void post(String url, Map<String, String> params, final HttpCallback<T> callback) {
Retrofit retrofit = create(url);
ApiService apiService = retrofit.create(ApiService.class);
Call<ResponseBody> call = apiService.post(url, params);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
try {
String result = response.body().string();
T data = new Gson().fromJson(result, callback.getType());
callback.onSuccess(data);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
callback.onFailure(t.getMessage());
}
});
}
}
```
8. 创建 ApiService 接口
ApiService 接口是 Retrofit 的请求接口,它定义了所有的网络请求方法。
```
public interface ApiService {
@GET
Call<ResponseBody> get(@Url String url, @QueryMap Map<String, String> params);
@FormUrlEncoded
@POST
Call<ResponseBody> post(@Url String url, @FieldMap Map<String, String> params);
}
```
9. 创建 HttpCallback 类
HttpCallback 类是网络请求回调接口,它定义了 onSuccess() 和 onFailure() 方法。
```
public interface HttpCallback<T> {
Type getType();
void onSuccess(T data);
void onFailure(String errorMsg);
}
```
至此,一个基于 MVP 模式的 Android 项目框架就搭建完成了,你可以在此基础上进行后续的开发工作。