原标题:Android开源项目-小熊日记讲解【附源码】

前言

一直是接着别人的代码来做App的,所以没有完整的开发App体验,总感觉有所缺少。于是,就利用业余时间亲自操刀开始了我的第一次开源项目。本文将讲述开源项目的整个开发过程。

项目初衷

开始这个项目的初衷是为了练习当前比较火热的框架。能够涉及到数据库存储,网络请求,图片加载等功能。于是,便以日记为主题,图文阅读为辅设计了如题的小熊日记。

项目功能

日记

趣闻

萌图


开发模式

开发模式选择MCP模式,如果你问我为什么?我不会告诉你的。

我就告诉你我使用后的一些感受吧,是用MVP代码量没有减少,但是在写代码前会思考的比较多,不用关注怎么实现。需要全局考虑功能页面有哪些功能,将所有的功能抽象出来。代码十分优雅,页面得功能能一目了然,后期维护也很方便

项目框架


网络层

volley和retrofit选择了retrofit,都是非常优秀的网络库,但是为了练习rxjava和lambda选择了retrofit虽然,retrofit的库要比volley大。

retrofit的整合

build.gradle文件:

compile'com.squareup.retrofit2:retrofit:2.2.0'
compile'com.squareup.retrofit2:converter-gson:2.2.0'
compile'com.squareup.retrofit2:retrofit-adapters:2.2.0'
compile'com.squareup.retrofit2:adapter-rxjava2:2.2.0'
compile'io.reactivex.rxjava2:rxandroid:2.0.1'
compile'io.reactivex.rxjava2:rxjava:2.0.9'

注意上面第四行的adapter-rxjava2:2.2.0中一定写上rxjava==2==,老的博客里面都是配置的是的是rxjava这里也是遇到各种坑,不写上2的话会报Unable to create call adapter for io.reactivex.Observable的异常导致闪退

AndroidManifest.xml配置步入正题,retrofit的初始化以及demo初始化retrofit,直接上代码publicclass ApiService{ //获取单例public static HttpInterface getApiService(){
returngetInstance().mHttpInterface; } //在访问HttpMethods时创建单例privatestaticclass SingletonHolder{
privatestaticfinalApiService INSTANCE = newApiService(); } private static ApiService getInstance(){
returnSingletonHolder.INSTANCE; } privateHttpInterface mHttpInterface; private ApiService(){ Retrofit retrofit = newRetrofit.Builder(). baseUrl("http://ent.sipmch.com.cn"). addConverterFactory(GsonConverterFactory.create()). addCallAdapterFactory(RxJava2CallAdapterFactory.create()). build(); mHttpInterface = retrofit.create(HttpInterface.class); }}

定义接口

publicinterfaceHttpInterface{
@FormUrlEncoded@POST("/ModuleDefaultCompany/RentManage/SearchRentNo/") Observable search(@Field("CertNo") String idNo);}

返回结果的JsonBean

publicclass RoomResult{
publicStringresult;
publicStringprompWord;}

方法调用

Apiservice.getService().search(account)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(roomResult -> {
Utils.showToast(mActivity,roomResult.prompWord);});

好了大功告成。

数据库

数据库选择GreenDao,库比较小,做文本储存绰绰有余。

gradle配置,外层需要全局引入GreenDao的插件:build{

repositories{ jcenter() } dependencies{

classpath'org.greenrobot:greendao-gradle-plugin:3.2.2'}}

然后在内部的gradle apply以及依赖相关的包

applyplugin: 'com.android.application'//applygreendao插件

applyplugin: 'org.greenrobot.greendao'compile'org.greenrobot:greendao:3.2.2'compile'net.zetetic:android-database-sqlcipher:3.5.1@aar'

上面compile 'net.zetetic:android-database-sqlcipher:3.5.1@aar'的导入也是在配置过程中遇到的坑后面会解释一下为什么加这一句。

greenDao配置及使用

首先在Application中初始化

publicclass MyApplication extends Application{
publicstaticMyApplication INSTANCE;
publicstaticfinalbooleanENCRYPTED = true;
privateDaoSession mDaoSession;
@Overridepublicvoid onCreate(){

super.onCreate(); INSTANCE = this; DaoMaster.DevOpenHelper help = newDaoMaster.DevOpenHelper(this, ENCRYPTED ? "bear-db-encrypted": "bear-db"); //此处有坑Database db = ENCRYPTED ? help.getEncryptedWritableDb("admin") : help.getWritableDb(); mDaoSession = newDaoMaster(db).newSession(); } publicDaoSession getDaoSession(){

returnmDaoSession; }}

下面建立一个数据表来试试,那先来个User表来试玩一下。新建UserBean class

@Entity(indexes = { //index 值是拿表中的account作为索引,且索引是唯一不可重复的@Index(value = "account",unique = true)})publicclassUserBean { //声明一个主键值@Idpubliclong id;

publicStringaccount;
publicStringpassWord;
publicStringmobile;
publicStringemail;}

建立完之后点一下Android Studio运行左边的小锤子make project后就会预编译出一个比较长的类了。

@Entity(indexes = {
@Index(value = "account",unique = true)})publicclass UserBean{
@Idpubliclongid;
publicString account;
publicString passWord;
publicString mobile;
publicString email;
@Generated(hash = 1328729131)public UserBean(long id, String account, String passWord, String mobile, String email){
this.id = id;
this.account = account;
this.passWord = passWord;
this.mobile = mobile;
this.email = email;}@Generated(hash = 1203313951)public UserBean(){}public long getId(){ returnthis.id;}public void setId(long id){ this.id = id;}public String getAccount(){ returnthis.account;}public void setAccount(String account){ this.account = account;}public String getPassWord(){ returnthis.passWord;}public void setPassWord(String passWord){ this.passWord = passWord;}public String getMobile(){ returnthis.mobile;}public void setMobile(String mobile){ this.mobile = mobile;}public String getEmail(){ returnthis.email;}public void setEmail(String email){ this.email = email;}}

Ok,表已经建立完毕,现在试着插条数据进去玩玩

UserBean userBean = new UserBean();
userBean.setId((long)1);
userBean.setAccount("18262282215");
userBean.setPassWord("111111qq");
userBean.setEmail("bear@berdatata.com");
userBean.setMobile("18262282215"); UserBeanDao dao = MyApplication.INSTANCE.getDaoSession().getUserBeanDao();

开始运行后,Crash 没错就是crash,wocao,明明是按照官网的配置一个个来的嘛。折腾几次终于发现少依赖了一个包,就是前面讲的使用加密的数据库需要导入compile 'net.zetetic:android-database-sqlcipher:3.5.1@aar'。总算是走通了。

图片加载(glide)

可以参考之前的文章:

项目数据源获取

趣闻来源

本着练习的心态,也是没有去找现有的API接口,去拉数据,于是学习了python爬去了糗百的热门段子,Node写了API接口。(注:本人只做个人练习,并不涉及商业使用)爬虫的编写,详情可点击左下角“阅读原文”查看。

图片来源

图片接口来自百度图片的接口。

运行效果图

其他

项目地址:https://github.com/MissMyDearBear/Diary

Debug 时间

Today's topic