当前位置: 首页 > >

111111111111

发布时间:


public class App extends Application{

private static App app;
private DaoMaster daoMaster;
private DaoSession daoSession;

@Override
public void onCreate() {
super.onCreate();
app = App.this;
Fresco.initialize(this);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "users", null);
SQLiteDatabase db = helper.getWritableDatabase();
daoMaster = new DaoMaster(db);
daoSession = daoMaster.newSession();
}
public static App getInstance(){
if (null == app){
synchronized (App.class){
if (null == app){
app = new App();
}
}
}
return app;
}

public DaoSession getDaoSession() {
return daoSession;
}
}

?


?


?



public class OKHttpUtils {
public static OKHttpUtils instance;
public OkHttpClient okHttpClient;

public OKHttpUtils(){
if (null == instance){
synchronized (OkHttpClient.class){
if (null == instance){
okHttpClient = new OkHttpClient.Builder()
.addInterceptor(new LoggingInterceptor())
.build();
}
}
}
}

public static OKHttpUtils getInstance() {
if (null == instance){
synchronized (OKHttpUtils.class){
if (null == instance){
instance = new OKHttpUtils();
}
}
}
return instance;
}

public void get(String urlString, Callback callback){
Request request = new Request.Builder().url(urlString).build();
okHttpClient.newCall(request).enqueue(callback);
}
public void post(String urlString, FormBody formBody,Callback callback){
Request request = new Request.Builder().url(urlString).method("POST",formBody).build();
okHttpClient.newCall(request).enqueue(callback);
}

class LoggingInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String method = request.method();
Log.d("LoggingInterceptor", method);
Response response = chain.proceed(request);
return response;
}
}
}

?


?



public class IModelImpl implements ICorter.IModel {
private static final String URL = "https://gank.io/api/data/%E7%A6%8F%E5%88%A9/10/1";
@Override
public void requestMsg(final onCallBack onCallBack) {
final UserDao userDao = App.getInstance().getDaoSession().getUserDao();
List users = userDao.loadAll();
if (users.size() > 0 ){
onCallBack.requestData(users);
return;
}
OKHttpUtils.getInstance().get(URL, new Callback() {
@Override
public void onFailure(Call call, IOException e) {
onCallBack.requestData(null);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
String responseMsg = response.body().string();
Gson gson = new Gson();
UserBean userBean = gson.fromJson(responseMsg, UserBean.class);
List results = userBean.getResults();
List userList = new ArrayList<>();
for (int i = 0; i < results.size() ; i++) {
String desc = results.get(i).getDesc();
String url = results.get(i).getUrl();
User user = new User();
user.setImgUrl(url);
user.setTitle(desc);
userList.add(user);
}
onCallBack.requestData(userList);
userDao.insertInTx(userList);
}
});
}
}



友情链接: