android_网络判断,Greendao,retrofit,eventbus结合展示数据

第一:导依赖

顶部:

apply plugin: 'org.greenrobot.greendao'

dependencies:
  compile 'org.greenrobot:greendao:3.2.2'
//    compile 'com.github.bumptech.glide:glide:4.3.1'
//    annotationProcessor 'com.github.bumptech.glide:compiler:4.3.1'
    compile 'com.jakewharton:butterknife:8.8.1'
    compile 'com.facebook.fresco:fresco:0.13.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
    compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'org.greenrobot:eventbus:3.1.1'
//repositories {
//    mavenCentral()
//    maven { url 'https://maven.google.com' }
//}

greendao{
    schemaVersion 1
    daoPackage 'com.bwei.www.renbaihui.gen'
    targetGenDir 'src/main/java'
}
网络判断工具类:
public class WifiUtils {
    private static volatile WifiUtils instance;

    private Context context;

    private WifiUtils(Context context) {
        this.context = context;
    }

    public static WifiUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (WifiUtils.class) {
                if (instance == null) {
                    instance = new WifiUtils(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;
    }
}

数据库工具类
public class DbUtils {
    private static volatile DbUtils instance;
    private final CateGoryDao dao;


    private DbUtils(Context context) {
        //创建数据库
        DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "category.db", null);
        SQLiteDatabase database = helper.getWritableDatabase();
        DaoMaster daoMaster = new DaoMaster(database);
        DaoSession daoSession = daoMaster.newSession();
        dao = daoSession.getCateGoryDao();
    }

    public static DbUtils getInstance(Context context){
        if (instance == null) {
            synchronized (DbUtils.class) {
                if (null == instance) {
                    instance = new DbUtils(context);
                }
            }
        }

        return instance;
    }

    public CateGoryDao getDao(){
       return dao;
    }
}
retrofit工具类:
public class RetrofitUtils {
    private static volatile RetrofitUtils instance;
    private final Retrofit retrofit;


    private RetrofitUtils(String baseurl) {
        //协议加域名
        //自动解析gson数据
        retrofit = new Retrofit.Builder()
                .baseUrl(baseurl)//协议加域名
                .addConverterFactory(GsonConverterFactory.create())//自动解析gson数据
                .build();
    }

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

    public Retrofit getretrofit(){
        return retrofit;
    }

}
EventBusMessage: 容器
public class EventBusMessage {
    private String msg;
    public EventBusMessage(String msg) {

       this.msg = msg;
    }
    public String getMsg(){
        return msg;
    }

    @Override
    public String toString() {
        return "EventBusMessage{" +
                "msg='" + msg + '\'' +
                '}';
    }
}

接口:
public interface Retro {
    @GET("data/Android/{id1}/{id2}")
    Call<MessageBean<List<Load>>> get(@Path("id1") int id1, @Path("id2") int id2);
}
CateGory:
@Entity
public class CateGory {
    @Id
    private Long id;
    private String title;
    private String time;
    @Generated(hash = 751089394)
    public CateGory(Long id, String title, String time) {
        this.id = id;
        this.title = title;
        this.time = time;
    }
    @Generated(hash = 240826954)
    public CateGory() {
    }
    public Long getId() {
        return this.id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTime() {
        return this.time;
    }
    public void setTime(String time) {
        this.time = time;
    }

}

适配器:
public class RAdapter extends RecyclerView.Adapter<RAdapter.ViewHolder> {

    Context context;
    List<CateGory> list;

    public RAdapter(Context context, List<CateGory> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.rv_item, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

//        Uri uri = Uri.parse("res://com.bwei.www.renbaihui.adapter/"+R.mipmap.ic_launcher_round);
//        holder.img.setImageURI(uri);
        holder.tv_tit.setText(list.get(position).getTitle());
        holder.tv_tim.setText(list.get(position).getTime());
    }

    @Override
    public int getItemCount() {

        return list.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

       // private final SimpleDraweeView img;
        private final TextView tv_tit;
        private final TextView tv_tim;

        public ViewHolder(View itemView) {
            super(itemView);
            //img = (SimpleDraweeView)itemView.findViewById(R.id.img);
            tv_tit = itemView.findViewById(R.id.tv_tit);
            tv_tim = itemView.findViewById(R.id.tv_tim);
        }
    }

}

MainActivity:
public class MainActivity extends AppCompatActivity {

    @butterknife.BindView(R.id.rv)
    RecyclerView rv;
    private CateGoryDao dao;
    List<CateGory> list = new ArrayList<>();
    private RAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //注册
        EventBus.getDefault().register(this);
        butterknife.ButterKnife.bind(this);
        dao = DbUtils.getInstance(this).getDao();
        List<CateGory> db = getDb();
        for (int i = 0; i < db.size(); i++) {
            Log.e("-----",db.get(i).getTime());
        }
        //创建适配器
        adapter = new RAdapter(MainActivity.this, db);
        if (db.size()==0){
            getNetData();
        }
        //条目添加线
        rv.addItemDecoration(new DividerItemDecoration(MainActivity.this, LinearLayoutManager.VERTICAL));
        LinearLayoutManager manager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.VERTICAL, false);
        rv.setLayoutManager(manager);
        rv.setAdapter(adapter);

        int netype = WifiUtils.getInstance(MainActivity.this).getNetype();
        if (netype == -1) {
            EventBus.getDefault().post(new EventBusMessage("当前无网络,将展示本地数据库数据"));
            //获取数据库数据
            getDb();
        } else if (netype == 1) {
            EventBus.getDefault().post(new EventBusMessage("当前wifi网络"));
            //从网络获取数据
            getNetData();
        } else {
            EventBus.getDefault().post(new EventBusMessage("当前手机网络"));
            //从网络获取数据
             getNetData();
        }
    }
//
public void getNetData(){
    final Retrofit getretrofit = RetrofitUtils.getInstance("http://gank.io/api/").getretrofit();
    Retro retro = getretrofit.create(Retro.class);
    Call<MessageBean<List<Load>>> call = retro.get(10, 1);
    call.enqueue(new Callback<MessageBean<List<Load>>>() {
        @Override
        public void onResponse(Call<MessageBean<List<Load>>> call, Response<MessageBean<List<Load>>> response) {
            MessageBean<List<Load>> body = response.body();
            List<Load> results = body.getResults();
            for(int i=0;i<results.size();i++) {
                list.add(new CateGory(null,results.get(i).getDesc(),results.get(i).getCreatedAt()));
                dao.insertOrReplace(list.get(i));
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<MessageBean<List<Load>>> call, Throwable t) {

        }
    });
}

    public List<CateGory> getDb() {
        List<CateGory> categories = dao.loadAll();
        //list.addAll(categories);
        return categories;
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBusMessage event) {
        /* Do something */
        Log.e("-=-=-=-", "onMessageEvent: " + event.getMsg());
        Toast.makeText(MainActivity.this, event.getMsg(), Toast.LENGTH_SHORT).show();
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}






 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
本系统的研发具有重大的意义,在安全性方面,用户使用浏览器访问网站时,采用注册和密码等相关的保护措施,提高系统的可靠性,维护用户的个人信息和财产的安全。在方便性方面,促进了校园失物招领网站的信息化建设,极大的方便了相关的工作人员对校园失物招领网站信息进行管理。 本系统主要通过使用Java语言编码设计系统功能,MySQL数据库管理数据,AJAX技术设计简洁的、友好的网址页面,然后在IDEA开发平台中,编写相关的Java代码文件,接着通过连接语言完成与数据库的搭建工作,再通过平台提供的Tomcat插件完成信息的交互,最后在浏览器中打开系统网址便可使用本系统。本系统的使用角色可以被分为用户和管理员,用户具有注册、查看信息、留言信息等功能,管理员具有修改用户信息,发布寻物启事等功能。 管理员可以选择任一浏览器打开网址,输入信息无误后,以管理员的身份行使相关的管理权限。管理员可以通过选择失物招领管理,管理相关的失物招领信息记录,比如进行查看失物招领信息标题,修改失物招领信息来源等操作。管理员可以通过选择公告管理,管理相关的公告信息记录,比如进行查看公告详情,删除错误的公告信息,发布公告等操作。管理员可以通过选择公告类型管理,管理相关的公告类型信息,比如查看所有公告类型,删除无用公告类型,修改公告类型,添加公告类型等操作。寻物启事管理页面,此页面提供给管理员的功能有:新增寻物启事,修改寻物启事,删除寻物启事。物品类型管理页面,此页面提供给管理员的功能有:新增物品类型,修改物品类型,删除物品类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值