ApI
apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.0" defaultConfig { applicationId "com.bwie.lenovo.xmer" minSdkVersion 15 targetSdkVersion 24 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } apply plugin: 'org.greenrobot.greendao' buildscript { repositories { mavenCentral() } dependencies { classpath 'org.greenrobot:greendao-gradle-plugin:3.0.0' } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:24.0.0' compile'org.greenrobot:greendao:3.0.1' compile'org.greenrobot:greendao-generator:3.0.0' compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4' compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:converter-scalars:2.0.0' compile 'com.android.support:recyclerview-v7:24.0.0' compile 'com.jakewharton:butterknife:7.0.1' compile 'com.facebook.fresco:fresco:0.11.0' compile 'org.greenrobot:eventbus:3.1.1' } greendao { schemaVersion 1 daoPackage 'com.bwie.lenovo.greendao' targetGenDir 'src/main/java' }
Mainctivity类package com.bwie.lenovo.xmer; import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Generated; @Entity public class NewslistBean { /** * ctime : 2017-12-01 * title : 缅怀梦老 | 如瑞法师:忆师教 * description : 五台山佛教 * picUrl : https://zxpic.gtimg.com/infonew/0/wechat_pics_-28168446.static/640 * url : https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111613&signature=yzT-gYh-rf2I9NYA5813ky0qHGSiy7m15rJPyay2QXWZTOcoC6iQQhNnH4H*UGPHOQ3gQJQ74fcil3uK1rh*HIL4VovPeQEMFOGeECn*n5E= */ @Id(autoincrement = true) private Long id; private String ctime; private String title; private String description; @Override public String toString() { return "NewslistBean{" + "ctime='" + ctime + '\'' + ", title='" + title + '\'' + ", description='" + description + '\'' + ", picUrl='" + picUrl + '\'' + ", url='" + url + '\'' + '}'; } private String picUrl; private String url; @Generated(hash = 1402622097) public NewslistBean(Long id, String ctime, String title, String description, String picUrl, String url) { this.id = id; this.ctime = ctime; this.title = title; this.description = description; this.picUrl = picUrl; this.url = url; } @Generated(hash = 923354944) public NewslistBean() { } public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPicUrl() { return picUrl; } public void setPicUrl(String picUrl) { this.picUrl = picUrl; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } }
package com.bwie.lenovo.xmer; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import com.bwie.lenovo.xmer.applaction.IApplaction; import com.bwie.lenovo.xmer.adapter.MyAdapter; import com.bwie.lenovo.xmer.persenter.IMainPersenter; import com.bwie.lenovo.xmer.view.IMainView; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity implements IMainView { @Bind(R.id.recyclerview) RecyclerView recyclerview; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); IMainPersenter mainPersenter = new IMainPersenter(this); mainPersenter.get(); LinearLayoutManager manager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false); recyclerview.setLayoutManager(manager); adapter = new MyAdapter(this); recyclerview.setAdapter(adapter); List<NewslistBean> list= IApplaction.session.getNewslistBeanDao().loadAll(); for(NewslistBean bean:list){ System.out.println(bean.toString()); } } @Override public void success(Bean bean) { System.out.println(bean.toString()); adapter.addData(bean); } @Override public void onFailure(Exception e) { } }SecondActivity类
package com.bwie.lenovo.xmer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Toast; import com.bwie.lenovo.EventBean; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); EventBus.getDefault().register(this); } @Subscribe(sticky = true) public void event(EventBean eventBean){ Toast.makeText(this, ""+ eventBean.getUrl() + " " + eventBean.getTitle(), Toast.LENGTH_SHORT).show(); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } }Model
package com.bwie.lenovo.xmer.model; import com.bwie.lenovo.xmer.Bean; import com.bwie.lenovo.xmer.applaction.IApplaction; import com.bwie.lenovo.xmer.ModelCallBack; import retrofit2.Call; import retrofit2.Callback; import retrofit2.Response; /** * Created by lenovo on 2017/12/1. */ public class MainModelImpl { private Call<Bean> call; public void getData(final ModelCallBack callBack){ call = IApplaction.iGerDataBase.get("18e883dd6b316eb1d97fd86338abbf06","10"); call.enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { Bean bean=response.body(); callBack.success(bean); //保存数据库 IApplaction.session.getNewslistBeanDao().insertInTx(bean.getNewslist()); } @Override public void onFailure(Call<Bean> call, Throwable t) { callBack.onFailure(new Exception("")); } }); } public void postData(final ModelCallBack callBack){ IApplaction.iGerDataBase.post("18e883dd6b316eb1d97fd86338abbf06","10"); call.enqueue(new Callback<Bean>() { @Override public void onResponse(Call<Bean> call, Response<Bean> response) { Bean bean = response.body(); callBack.success(bean); } @Override public void onFailure(Call<Bean> call, Throwable t) { callBack.onFailure(new Exception("")); } }); } }View
package com.bwie.lenovo.xmer.view; import com.bwie.lenovo.xmer.Bean; /** * Created by lenovo on 2017/12/1. */ public interface IMainView { public void success(Bean bean); public void onFailure(Exception e); }Persenter
package com.bwie.lenovo.xmer.persenter; import com.bwie.lenovo.xmer.Bean; import com.bwie.lenovo.xmer.ModelCallBack; import com.bwie.lenovo.xmer.model.MainModelImpl; import com.bwie.lenovo.xmer.view.IMainView; /** * Created by lenovo on 2017/12/1. */ public class IMainPersenter { private IMainView iMainView; private MainModelImpl mainModel; public IMainPersenter(IMainView iMainView){ this.iMainView=iMainView; mainModel=new MainModelImpl(); } public void get(){ mainModel.getData(new ModelCallBack() { @Override public void success(Bean bean) { if (iMainView!=null){ iMainView.success(bean); } } @Override public void onFailure(Exception e) { if(iMainView!=null){ iMainView.onFailure(e); } } }); } public void post(){ mainModel.postData(new ModelCallBack() { @Override public void success(Bean bean) { if (iMainView!=null){ iMainView.success(bean); } } @Override public void onFailure(Exception e) { if(iMainView!=null){ iMainView.onFailure(e); } } }); } }IApplaction
package com.bwie.lenovo.xmer.applaction; import android.app.Application; import com.bwie.lenovo.greendao.DaoMaster; import com.bwie.lenovo.greendao.DaoSession; import com.bwie.lenovo.xmer.IGerDataBase; import com.facebook.drawee.backends.pipeline.Fresco; import org.greenrobot.greendao.database.Database; import retrofit2.Retrofit; import retrofit2.converter.gson.GsonConverterFactory; /** * Created by lenovo on 2017/12/1. */ public class IApplaction extends Application { public static IGerDataBase iGerDataBase; public static DaoSession session; @Override public void onCreate() { super.onCreate(); //注册 Fresco.initialize(this); Retrofit retrofit = new Retrofit.Builder().baseUrl("http://api.tianapi.com") .addConverterFactory(GsonConverterFactory.create()) .build(); iGerDataBase = retrofit.create(IGerDataBase.class); DaoMaster.DevOpenHelper helper= new DaoMaster.DevOpenHelper(this,"test"); Database database = helper.getWritableDb(); session = new DaoMaster(database).newSession(); } }Adapter
package com.bwie.lenovo.xmer.adapter; import android.content.Context; import android.content.Intent; import android.support.v7.widget.RecyclerView; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import com.bwie.lenovo.EventBean; import com.bwie.lenovo.xmer.Bean; import com.bwie.lenovo.xmer.NewslistBean; import com.bwie.lenovo.xmer.R; import com.bwie.lenovo.xmer.SecondActivity; import com.facebook.drawee.view.SimpleDraweeView; import org.greenrobot.eventbus.EventBus; import java.util.ArrayList; import java.util.List; import butterknife.Bind; import butterknife.ButterKnife; /** * Created by lenovo on 2017/12/1. */ public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { Context context; List<NewslistBean> list; public MyAdapter(Context context) { this.context = context; } public void addData(Bean bean){ if (list==null){ list=new ArrayList<>(); } // System.out.println(list); list.addAll(bean.getNewslist()); notifyDataSetChanged(); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = View.inflate(context, R.layout.activity_adapter, null); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, final int position) { System.out.println("ppp"+list.size()); holder.simpleDraweeView.setImageURI(list.get(position).getPicUrl()); System.out.println("image = " + list.get(position).getPicUrl()); holder.textview.setText(list.get(position).getTitle()); System.out.println("title = " + list.get(position).getTitle()); holder.textview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { EventBus.getDefault().postSticky(new EventBean(list.get(position).getPicUrl(),list.get(position).getTitle())); context.startActivity(new Intent(context, SecondActivity.class)); } }); } @Override public int getItemCount() { return list==null ? 0:list.size(); } public class ViewHolder extends RecyclerView.ViewHolder { @Bind(R.id.simpleDraweeView) SimpleDraweeView simpleDraweeView; @Bind(R.id.textview) TextView textview; public ViewHolder(View itemView) { super(itemView); ButterKnife.bind(this,itemView); } } }Bean
package com.bwie.lenovo.xmer; import java.util.List; /** * Created by lenovo on 2017/12/1. */ public class Bean { /** * code : 200 * msg : success * newslist : [{"ctime":"2017-12-01","title":"缅怀梦老 | 如瑞法师:忆师教","description":"五台山佛教","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-28168446.static/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111613&signature=yzT-gYh-rf2I9NYA5813ky0qHGSiy7m15rJPyay2QXWZTOcoC6iQQhNnH4H*UGPHOQ3gQJQ74fcil3uK1rh*HIL4VovPeQEMFOGeECn*n5E="},{"ctime":"2017-12-01","title":"巅峰艺术 | 画家陈溪峋","description":"巅峰艺术","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-60618301.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111613&signature=nGMTpupDKtubaUO5f8BBR6Fy4jExDScpnXMGu99ngFtz-8u9kMzveVmKkjYJaUhO2tD6UrN8uDWqK6D0Sx8d5Ss9rVgc4NVfYv0dpOI3sNM="},{"ctime":"2017-12-01","title":"【文化】专访故宫博物院院长单霁翔:我们希望能够把一个壮美的紫禁城,完整地交给下一个600年","description":"检察日报","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-59241555.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111612&signature=7tE60OlBAQPfaqUzFbd5eOSUos6sESKesUhBAdfc7taQVFTQ8GhsAHNXpMwb1FC2HVo5cHcE-oCj9f4jxv-cf5om6WRqQ00-fLN5PFtL6L0="},{"ctime":"2017-12-01","title":"被画画耽误的哲学家们居然组成了一个流派!","description":"芭莎艺术","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-60618007.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111612&signature=pxHUobPrCezO6PR1Vc3JF45xkBDwu7UA558NX98N6XQpqPjRi*To2fzf74TNEtfONc46*OzXMwYiNLS-1VtugDdxUJw7Ov0epOKO11qe*EY="},{"ctime":"2017-12-01","title":"潮汕民居 | 第三届凤凰民俗摄影双年展\u201c金凤凰\u201d优秀展览","description":"中国摄影报","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-60617433.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111612&signature=oz7CzGRbJLbn1Wa3OFki-1ZwOJzuyeDpa7r7ufass6fQmkJraUIqmdTKFQihAjGuLPtJhtGCOc5GeiFigj5QbBgIDBvHq8zI*9OJwj*D4kg="},{"ctime":"2017-12-01","title":"创作心语 | 创作出\u201c有嚼头\u201d的电视剧","description":"人民日报文艺","picUrl":"https://t1.qpic.cn/mblogpic/f01a972dbcc1060fd456/2000","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111611&signature=WdvRWCjzt*pxY8kWV*PswkhFm92r97G2aGHgCHUIFd4vS3DN*lQVSnIP1uxhKYNBhQrgydFvbkPbAuK8YRNIkxoj57XekLYIX2ozRC0JUHQ="},{"ctime":"2017-12-01","title":"假装生病没去上学,突然听到爸妈回家声音时的你","description":"萌宠部落","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-60617537.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111611&signature=K3Hch-*HywUTWZceskJd717rxwZbGJbX5znqI3lYRf5I0yJ8x0JBFxzJbcsyMmn6RCnBxu7bGKeydXlgC5sVzpAOb*hWZ5mm8xM-HImLsXI="},{"ctime":"2017-12-01","title":"如果你是欧阳娜娜,是苦磕大提琴,还是竞走娱乐圈?","description":"三联生活周刊","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-60615306.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111611&signature=UzN12WAaSNyGFa6O9d13v4fe5J82YXpI1TAPvUtIrwUgt4O6lRtWbK6VLugl6Q*3GnYtobW*1vSgtuj2XVPNw-04bf51icwagiyVWslDkhY="},{"ctime":"2017-12-01","title":"相见恨晚的好书!十大名著早就该这么读","description":"南山子春秋","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-59275008.jpg/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111610&signature=QwJBWNPc36WhGpPTTRdrkdHqfc*sGLiUeB8Wkrm-XRajQheVVMmf*ZRVfkQBnYrELEMWYuk6YXxELMxztTV95srTjwGT35X6BXDrJ3mUsIc="},{"ctime":"2017-12-01","title":"\u201c你再敢动我孩子试试\u201d:儿子被强奸后,父亲一枪崩掉了强奸犯,法院的判决出乎意料","description":"百年树人","picUrl":"https://zxpic.gtimg.com/infonew/0/wechat_pics_-37619597.static/640","url":"https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111610&signature=u3bvFnn2InqjfV0C2P2fUJWTwHDAxlDTyI0cf3kczKgB*vh32XP-GFNp6Ox8hcpMSDxjyCQJWrAOfOGa-G4*RlugP67UMnz1FxoqMO2Hzco="}] */ private int code; private String msg; private List<NewslistBean> newslist; public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public List<NewslistBean> getNewslist() { return newslist; } public void setNewslist(List<NewslistBean> newslist) { this.newslist = newslist; } }
IGerDataBase类Retrofit的网络请求到MainModelImpl 类中做任何操作
package com.bwie.lenovo.xmer; import retrofit2.Call; import retrofit2.http.Field; import retrofit2.http.GET; import retrofit2.http.POST; import retrofit2.http.Query; /** * Created by lenovo on 2017/12/1. */ public interface IGerDataBase { /** * get * @param key * @return */ @GET("wxnew") Call<Bean> get(@Query("key") String key,@Query("num") String num); /** * post * @param key * @return */ @POST("wxnew") Call<Bean> post(@Field("key") String key,@Query("num") String num); }NewsListBean类ModelCallBack类package com.bwie.lenovo.xmer; import org.greenrobot.greendao.annotation.Entity; import org.greenrobot.greendao.annotation.Id; import org.greenrobot.greendao.annotation.Generated; @Entity public class NewslistBean { /** * ctime : 2017-12-01 * title : 缅怀梦老 | 如瑞法师:忆师教 * description : 五台山佛教 * picUrl : https://zxpic.gtimg.com/infonew/0/wechat_pics_-28168446.static/640 * url : https://mp.weixin.qq.com/s?src=16&ver=522×tamp=1512111613&signature=yzT-gYh-rf2I9NYA5813ky0qHGSiy7m15rJPyay2QXWZTOcoC6iQQhNnH4H*UGPHOQ3gQJQ74fcil3uK1rh*HIL4VovPeQEMFOGeECn*n5E= */ @Id(autoincrement = true) private Long id; private String ctime; private String title; private String description; @Override public String toString() { return "NewslistBean{" + "ctime='" + ctime + '\'' + ", title='" + title + '\'' + ", description='" + description + '\'' + ", picUrl='" + picUrl + '\'' + ", url='" + url + '\'' + '}'; } private String picUrl; private String url; @Generated(hash = 1402622097) public NewslistBean(Long id, String ctime, String title, String description, String picUrl, String url) { this.id = id; this.ctime = ctime; this.title = title; this.description = description; this.picUrl = picUrl; this.url = url; } @Generated(hash = 923354944) public NewslistBean() { } public String getCtime() { return ctime; } public void setCtime(String ctime) { this.ctime = ctime; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getPicUrl() { return picUrl; } public void setPicUrl(String picUrl) { this.picUrl = picUrl; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } }
public interface ModelCallBack { public void success(Bean bean); public void onFailure(Exception e); }
访问权限
<uses-permission android:name="android.permission.INTERNET" />
android:name=".applaction.IApplaction"