public class OkHttpUtil {
private static OkHttpUtil okHttpUtil;
private static OkHttpClient okHttpClient;
private OkHttpUtil(){
okHttpClient = new OkHttpClient();
}
public static synchronized OkHttpClient getInstance(){
if (null==okHttpUtil){
okHttpUtil = new OkHttpUtil();
}
return okHttpClient;
}
}數據庫
主進程 調用api
private void initData() {
// OkHttpClient okHttpClient = new OkHttpClient();
OkHttpClient okHttpClient = OkHttpUtil.getInstance();
Request request = new Request.Builder().url(URL).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {ide
}ui
@Override
public void onResponse(Call call, Response response) throws IOException {
String string = response.body().string();
handler.obtainMessage(1, string).sendToTarget();
}
});this
}url
private List<Bean.ParamzBean.FeedsBean> list1;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what){
case 1:
String s = (String) msg.obj;
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
//查詢
UserDao userDao = App.getAoo().daoSession().getUserDao();
List<User> list = userDao.loadAll();
if (list.size()<=0) {
//數據庫插入數據
for (int i = 0; i < bean.getParamz().getFeeds().size(); i++) {
User user = new User();
user.setImg("http://litchiapi.jstv.com" + bean.getParamz().getFeeds().get(i).getData().getCover());
user.setName(bean.getParamz().getFeeds().get(i).getData().getSubject());
user.setText(bean.getParamz().getFeeds().get(i).getData().getSummary());
Log.i("login",bean.getParamz().getFeeds().get(i).getData().getSubject()+"-------------");
userDao.insert(user);
}
initAdapter();
}else {
initAdapter();
}
}
}
};
///刷新查詢.net
private void initAdapter() {
UserDao userDao = App.getAoo().daoSession().getUserDao();
List<User> list = userDao.loadAll();
adapter = new Adapter(this, list);
lv.setAdapter(adapter);
}進程