导入依赖
implementation 'com.squareup.retrofit2:retrofit:2.4.0'
implementation 'com.squareup.retrofit2:converter-gson:2.4.0'
implementation 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
implementation 'io.reactivex:rxandroid:1.2.1'
implementation 'io.reactivex:rxjava:1.1.6'
implementation 'com.squareup.okhttp3:logging-interceptor:3.5.0'
创建retrofit管理类
public class RetrofitManager {
public static OkHttpClient okHttpClient;
private static RetrofitManager instance ;
private String baseUrl;
private Retrofit retrofit;
private RetrofitManager(String baseUrl){
this.baseUrl = baseUrl;
initRetrofit();
}
/**
* 静态块
*/
static {
getOkhttpClient();
}
public static synchronized RetrofitManager getInstance(String baseUrl){
if (instance == null){
synchronized (RetrofitManager.class){
if (instance == null){
instance = new RetrofitManager(baseUrl);
}
}
}
return instance;
}
/**
* 单例模式获取Okhttp
*/
private static OkHttpClient getOkhttpClient() {
if (okHttpClient == null){
synchronized (OkHttpClient.class){
if (okHttpClient==null){
okHttpClient = new OkHttpClient.Builder()
.addNetworkInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
.connectTimeout(3000,TimeUnit.SECONDS)
.readTimeout(3000,TimeUnit.SECONDS)
.build();
}
}
}
return okHttpClient;
}
private void initRetrofit(){
retrofit = new Retrofit.Builder()
.baseUrl(ApiService.BASE_URL)
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public <T> T setCreate(Class<T> reqServer){
return retrofit.create(reqServer);
}
}
在application中初始化,注:在AndroidManifest.xml中的name注册
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
//初始化fresco
Fresco.initialize(this);
}
}
创建model接口
public interface IHomeModel {
void getData(String url,int page,int count,String keyword,ModelInterface modelInterface);
interface ModelInterface{
void loadSuccess(Object o);
void loadFailed();
}
}
创建mdel实现类
public class HomeModel implements IHomeModel {
@Override
public void getData(String url, int page, int count, String keyword, final ModelInterface modelInterface) {
IApiService iApiService = RetrofitManager.getInstance(ApiService.BASE_URL).setCreate(IApiService.class);
Call<GoodsBean> goodsBeanCall = iApiService.showGoods(url, page, count, keyword);
goodsBeanCall.enqueue(new Callback<GoodsBean>() {
@Override
public void onResponse(Call<GoodsBean> call, Response<GoodsBean> response) {
GoodsBean goodsBean = response.body();
modelInterface.loadSuccess(goodsBean);
}
@Override
public void onFailure(Call<GoodsBean> call, Throwable t) {
modelInterface.loadFailed();
}
});
}
}
创建persenter接口
public interface IHomePersenter {
void getPersenterData(int page,int count,String keyword);
}
创建persenter实现类
public class HomePersenter implements IHomePersenter {
MainActivity mainActivity;
private final HomeModel homeModel;
public HomePersenter(MainActivity mainActivity) {
this.mainActivity = mainActivity;
homeModel = new HomeModel();
}
@Override
public void getPersenterData(int page, int count, String keyword) {
homeModel.getData(ApiService.GOODS_INFORMATION, page, count, keyword, new IHomeModel.ModelInterface() {
@Override
public void loadSuccess(Object o) {
mainActivity.getViewData(o);
}
@Override
public void loadFailed() {
}
});
}
}
创建view接口
public interface IHomeView {
void getViewData(Object o);
}
activit类实现view接口
public class MainActivity extends AppCompatActivity implements IHomeView {
@BindView(R.id.edit_sou)
EditText editSou;
@BindView(R.id.text_sou)
TextView textSou;
@BindView(R.id.xrecycler_view)
XRecyclerView xrecyclerView;
private MyAdapter adapter;
private HomePersenter homePersenter;
private String str;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//去掉Activity上面的状态栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
ButterKnife.bind(this);
homePersenter = new HomePersenter(MainActivity.this);
//创建布局
GridLayoutManager gridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
xrecyclerView.setLayoutManager(gridLayoutManager);
homePersenter.getPersenterData(page, count, keyword);
}
@Override
public void getViewData(Object o) {
final GoodsBean goodsBean = (GoodsBean) o;
adapter = new MyAdapter(MainActivity.this, goodsBean);
xrecyclerView.setAdapter(adapter);
}
@OnClick(R.id.text_sou)
public void onViewClicked() {
str = editSou.getText().toString();
if (TextUtils.isEmpty(str)) {
Toast.makeText(this, "请输入搜索内容", Toast.LENGTH_SHORT).show();
} else {
homePersenter.getPersenterData(page, count,str);
adapter.notifyDataSetChanged();
}
}
}