public interface UserContract {
interface IView{
void User_Success(List<UserEntity.ResultBean> beans);
void failure(String msg);
}
interface IModel{
void getList(String url,HashMap<String,String> params,ModelCallback modelCallback);
}
public abstract class IPresenter{
public abstract void getList(String url,HashMap<String, String> params);
}
public interface ModelCallback{
void success(List<UserEntity.ResultBean> beans);
void failure(String msg);
}
}
public class UserModel implements UserContract.IModel {
@Override
public void getList(String url, HashMap<String, String> params, final UserContract.ModelCallback modelCallback) {
Api api = RetrofitUtils.getInstance().api();
Call<UserEntity> userEntityCall = api.UserShow(url, params);
userEntityCall.enqueue(new Callback<UserEntity>() {
@Override
public void onResponse(Call<UserEntity> call, Response<UserEntity> response) {
if (modelCallback!=null){
modelCallback.success(response.body().getResult());
}
}
@Override
public void onFailure(Call<UserEntity> call, Throwable t) {
if (modelCallback!=null){
modelCallback.failure(t.getMessage());
}
}
});
}
}
public class UserPresenter extends UserContract.IPresenter {
UserModel userModel;
UserContract.IView iView;
public UserPresenter(UserContract.IView iView) {
userModel=new UserModel();
this.iView = iView;
}
@Override
public void getList(String url, HashMap<String, String> params) {
userModel.getList(url, params, new UserContract.ModelCallback() {
@Override
public void success(List<UserEntity.ResultBean> beans) {
iView.User_Success(beans);
}
@Override
public void failure(String msg) {
iView.failure(msg);
}
});
}
}
Retrofit的工具类
public class RetrofitUtils {
private static RetrofitUtils instance;
private final Retrofit retrofit;
private RetrofitUtils(){
HttpLoggingInterceptor loggingInterceptor=new HttpLoggingInterceptor();
loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
OkHttpClient okHttpClient=new OkHttpClient.Builder()
.addNetworkInterceptor(loggingInterceptor)
.addInterceptor(loggingInterceptor)
.build();
retrofit= new Retrofit.Builder()
.baseUrl(UserApi.USER_API)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
}
public static RetrofitUtils getInstance() {
if (instance==null){
synchronized (RetrofitUtils.class){
if (instance==null){
instance = new RetrofitUtils();
}
}
}
return instance;
}
public Api api(){
return retrofit.create(Api.class);
}
}
Retrofit的api
public interface Api {
// @POST
// @FormUrlEncoded
// <T>
// Call<T> Show(@Url String uri, @FieldMap HashMap<String,String> map);
@GET
Call<UserEntity> UserShow(@Url String uri, @QueryMap HashMap<String,String> map);
@GET
Call<XqEntity> XqShow(@Url String uri, @QueryMap HashMap<String,String> map);
}
butterknife绑定要一定要记得写
ButterKnife.bind(this);
数据库首先先
@Entity//至关重要写完后点击小锤子自动生成
public class MySql {
@Id(autoincrement = true)//自增的
@Unique//唯一的
private Long id;//主键
private String name;
private String pic;
private String pice;
private int CommodityId;
初始化在appliaction中
Fresco.initialize(this);
GreenDaoUtils.getInstance().initGreenDao(this);
创建greendao工具类
public class GreenDaoUtils {
private static GreenDaoUtils mInstance;
private GreenDaoUtils(){
}
/**
* 双重检索锁
* @return
*/
public static GreenDaoUtils getInstance(){
if (mInstance==null){
synchronized (GreenDaoUtils.class){
if (mInstance==null){
mInstance = new GreenDaoUtils();
}
}
}
return mInstance;
}
/**
* 初始化GreenDao,直接在Application中进行初始化操作
*/
public void initGreenDao(Context context) {
//创建daomaster
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, Constants.DB_NNAME);
SQLiteDatabase db = helper.getWritableDatabase();//db 读写数据库
DaoMaster daoMaster = new DaoMaster(db);//创建对象
//创建daosession
daoSession = daoMaster.newSession();
}
private DaoSession daoSession;
public DaoSession getDaoSession() {
return daoSession;
}
}
Fresco自定义缓存路径
DiskCacheConfig diskCacheConfig=DiskCacheConfig.newBuilder(this).setBaseDirectoryName("images").setBaseDirectoryPath(Environment.getExternalStorageDirectory()).build();
ImagePipelineConfig imagePipelineConfig=ImagePipelineConfig.newBuilder(this).setMainDiskCacheConfig(diskCacheConfig).build();
Fresco.initialize(this,imagePipelineConfig);