//MainActivity代码
package com.example.zhoukaoyi;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.zhoukaoyi.adapter.Rvadapter;
import com.example.zhoukaoyi.bean.Bean;
import com.example.zhoukaoyi.bean.Bh;
import com.example.zhoukaoyi.bean.Xiaobean;
import com.example.zhoukaoyi.gen.BhDao;
import com.example.zhoukaoyi.utils.DbHelper;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.util.ArrayList;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by o
*/
public class fragment1 extends Fragment {
private ApiService service1;
private Rvadapter adapters;
RecyclerView rv;
private List<Bh> list = new ArrayList<>();
private BhDao xiaobeanDao;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
EventBus.getDefault().register(this);
View view = inflater.inflate(R.layout.fragment1, container, false);
rv=view.findViewById(R.id.rv);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//数据库
xiaobeanDao = DbHelper.getInstance(getActivity()).getXiaobeanDao();
LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
rv.setLayoutManager(manager);
adapters = new Rvadapter(getActivity(), list);
rv.setAdapter(adapters);
//判断网络
int netype = WifeUtils.getInstance(getActivity()).getNetype();
if (netype==-1){
EventBus.getDefault().post("没网");
getORMData();
}else{//有网
EventBus.getDefault().post("有网");
getNetdata();
}
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(String event){
Toast.makeText(getActivity(),event,Toast.LENGTH_SHORT).show();
}
public void getORMData(){
//得到数据库数据
List<Bh> bhs = xiaobeanDao.loadAll();
//填装到展示的集合
list.addAll(bhs);
adapters.notifyDataSetChanged();
}
//网络请求
public void getNetdata(){
//获取接口地址
Retrofit retrofit = new Retrofit.Builder()
//自动Gson解析
.addConverterFactory(GsonConverterFactory.create())
.baseUrl("http://gank.io/")
.build();
//获取类类型对象
service1 = retrofit.create(ApiService.class);
//接口中的bean类
Call<Bean<List<Xiaobean>>> call = service1.GroupList(10,1);
call.enqueue(new Callback<Bean<List<Xiaobean>>>() {
@Override
public void onResponse(Call<Bean<List<Xiaobean>>> call, Response<Bean<List<Xiaobean>>> response) {
//通过流的形式获取数据
Bean<List<Xiaobean>> body = response.body();
List<Xiaobean> results = body.getResults();
//清空数据库
xiaobeanDao.deleteAll();
//实例化数据类型对象
Bh bh = new Bh();
for(int i=0;i<results.size();i++){
bh.setId(null);
bh.setH(results.get(i).getCreatedAt());
bh.setWb(results.get(i).getDesc());
list.add(new Bh(null,results.get(i).getDesc(),results.get(i).getCreatedAt()));
//添加到数据库
xiaobeanDao.insert(bh);
}
//刷新适配器
adapters.notifyDataSetChanged();
}
@Override
public void onFailure(Call<Bean<List<Xiaobean>>> call, Throwable t) {
}
});
}
@Override
public void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
/////////////////////////////////////////////获取路径需要创建接口
package com.example.zhoukaoyi;
import com.example.zhoukaoyi.bean.Bean;
import com.example.zhoukaoyi.bean.Xiaobean;
import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;
/**
* Created by
*/
public interface ApiService { //参数地址
@GET("api/data/Android/{id1}/{id2}")
Call<Bean<List<Xiaobean>>> GroupList(@Path("id1") int id1,@Path("id2") int id2);
//参数拼接
// @GET("api/data/Android")
// Call<Bean<List<Xiaobean>>> GroupList(@Query("uid") int uid,@Query("name") String name);
}
//////////////////////////////////适配器
package com.example.zhoukaoyi.adapter;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.zhoukaoyi.R;
import com.example.zhoukaoyi.bean.Bh;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
/**
* Created
*/
//适配器
public class Rvadapter extends RecyclerView.Adapter<Rvadapter.ViewHolder>{
private Context context;
private List<Bh> list;
public Rvadapter(Context context, List<Bh> list) {
this.context = context;
this.list = list;
}
@Override
public Rvadapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = View.inflate(context, R.layout.rv_fragment, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(Rvadapter.ViewHolder holder, int position) {
holder.title.setText(list.get(position).getWb());
holder.type.setText(list.get(position).getH());
holder.img.setImageURI("http://www.cndog.net/tpb/2013-12/14/14970-6031.jpg");
}
@Override
public int getItemCount() {
return list.size();
}
class ViewHolder extends RecyclerView.ViewHolder{
private final SimpleDraweeView img;
private final TextView title;
private final TextView type;
public ViewHolder(View itemView) {
super(itemView);
img =itemView.findViewById(R.id.img);
title =itemView.findViewById(R.id.title);
type =itemView.findViewById(R.id.type);
}
}
}
/////////////////////////////////数据库的封装类工具类
package com.example.zhoukaoyi.utils;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.example.zhoukaoyi.gen.BhDao;
import com.example.zhoukaoyi.gen.DaoMaster;
import com.example.zhoukaoyi.gen.DaoSession;
/**
* Created by
*/
public class DbHelper {
private static volatile DbHelper instance;
private final DaoSession daoSession;
private final DaoMaster daoMaster;
private DbHelper(Context context){
DaoMaster.DevOpenHelper xiaokai = new DaoMaster.DevOpenHelper(context, "zhoukaoyi", null);
SQLiteDatabase writableDatabase = xiaokai.getWritableDatabase();
daoMaster = new DaoMaster(writableDatabase);
daoSession = daoMaster.newSession();
}
public static DbHelper getInstance(Context context){
if (instance==null){
synchronized (DbHelper.class){
if (null==instance){
instance=new DbHelper(context);
}
}
}
return instance;
}
//将daoSessionDao返回
public BhDao getXiaobeanDao() {
return daoSession.getBhDao();
}
}
///////////////////////////////////////网络判断工具类
package com.example.zhoukaoyi;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* Created by
*/
//判断网络
public class WifeUtils {
private static volatile WifeUtils instance;
private Context context;
private WifeUtils(Context context) {
this.context = context;
}
public static WifeUtils getInstance(Context context) {
if (instance == null) {
synchronized (WifeUtils.class) {
if (instance == null) {
instance = new WifeUtils(context);
}
}
}
return instance;
}
public int getNetype() {
int netType = -1;
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
//无网络
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
//手机网络
if (nType == ConnectivityManager.TYPE_MOBILE) {
netType = 2;
} else if (nType == ConnectivityManager.TYPE_WIFI) {//wifi网络
netType = 1;
}
//返回
return netType;
}
}
//////////////////////////////////泛型Bean类
package com.example.zhoukaoyi.bean;
/**
* Created by
*/
public class Bean<T> {
private boolean error;
private T results;
public boolean isError() {
return error;
}
public void setError(boolean error) {
this.error = error;
}
public T getResults() {
return results;
}
public void setResults(T results) {
this.results = results;
}
}
/////////////////////////////////自己创建的类 需要的属性
package com.example.zhoukaoyi.bean;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
/**
* Cr.
*/
@Entity
public class Bh {
@Id
private Long id;
private String Wb;
private String h;
@Generated(hash = 329677494)
public Bh(Long id, String Wb, String h) {
this.id = id;
this.Wb = Wb;
this.h = h;
}
@Generated(hash = 1817500822)
public Bh() {
}
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getWb() {
return this.Wb;
}
public void setWb(String Wb) {
this.Wb = Wb;
}
public String getH() {
return this.h;
}
public void setH(String h) {
this.h = h;
}
}