classpath 'org.greenrobot:greendao-gradle-plugin:3.2.2'
compile 'org.greenrobot:greendao:3.2.2'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.facebook.fresco:fresco:1.5.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
compile 'org.greenrobot:eventbus:3.0.0'
compile 'com.android.support:recyclerview-v7:26.1.0'
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
greendao {
schemaVersion 1
daoPackage 'com.afa.tourism.greendao.gen'
targetGenDir 'src/main/java'
}
build.gradel添加头部:
apply plugin: 'org.greenrobot.greendao'
添加到依赖上面
greendao { schemaVersion 1 daoPackage 'com.afa.tourism.greendao.gen' targetGenDir 'src/main/java' }
activity_main布局
IApplication类配置
package com.example.administrator.myapplication;
import android.app.Application;
import com.afa.tourism.greendao.gen.DaoMaster;
import com.afa.tourism.greendao.gen.DaoSession;
import com.facebook.drawee.backends.pipeline.Fresco;
import org.greenrobot.greendao.database.Database;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* Created by Administrator on 2017/12/1.
*/
//http://api.tianapi.com/wxnew/?key=18e883dd6b316eb1d97fd86338abbf06&num=10
public class IApplication extends Application{
public static IGetDataBase iGetDataBase;
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();
iGetDataBase = retrofit.create(IGetDataBase.class);
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "test");
Database database = helper.getWritableDb();
session = new DaoMaster(database).newSession();
}
}
package com.example.administrator.myapplication;
import com.example.administrator.myapplication.bean.Bean;
import retrofit2.Call;
import retrofit2.http.Field;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;
/**
* Created by Administrator on 2017/12/1.
*/
//http://api.tianapi.com/wxnew/?key=18e883dd6b316eb1d97fd86338abbf06&num=10
public interface IGetDataBase {
@GET("/wxnew/")
Call get(@Query("key") String key,@Query("num") int num);
/*@POST("/wxnew/")
Call post(@Field("key") String key, int num);*/
}
Model层
MainCallBack接口
package com.example.administrator.myapplication.model;
import com.example.administrator.myapplication.bean.Bean;
/**
* Created by Administrator on 2017/12/1.
*/
public interface MainCallBack {
public void success(Bean bean);
public void fail(Exception e);
}
MyModel类
package com.example.administrator.myapplication.model;
import com.example.administrator.myapplication.IApplication;
import com.example.administrator.myapplication.bean.Bean;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by Administrator on 2017/12/1.
*/
//http://api.tianapi.com/wxnew/?key=18e883dd6b316eb1d97fd86338abbf06&num=10
public class MainModel {
public void getData(final MainCallBack callBack){
Call call= IApplication.iGetDataBase.get("18e883dd6b316eb1d97fd86338abbf06",10);
call.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
Bean bean = response.body();
callBack.success(bean);
IApplication.session.getNewslistBeanDao().insertInTx(bean.getNewslist());
}
@Override
public void onFailure(Call call, Throwable t) {
callBack.fail(new Exception(""));
}
});
}
}
View层
IMainInterface接口
package com.example.administrator.myapplication.view;
import com.example.administrator.myapplication.bean.Bean;
/**
* Created by Administrator on 2017/12/1.
*/
public interface IMaininterface {
public void success(Bean bean);
public void fail(Exception e);
}
MyPresenter类
package com.example.administrator.myapplication.presenter;
import com.example.administrator.myapplication.bean.Bean;
import com.example.administrator.myapplication.model.MainCallBack;
import com.example.administrator.myapplication.model.MainModel;
import com.example.administrator.myapplication.view.IMaininterface;
/**
* Created by Administrator on 2017/12/1.
*/
public class MainPresenter {
private IMaininterface iMaininterface;
private MainModel mainModel;
public MainPresenter(IMaininterface iMaininterface){
this.iMaininterface=iMaininterface;
this.mainModel=new MainModel();
}
public void get(){
mainModel.getData(new MainCallBack() {
@Override
public void success(Bean bean) {
if(iMaininterface!=null){
iMaininterface.success(bean);
}
}
@Override
public void fail(Exception e) {
if(iMaininterface!=null){
iMaininterface.fail(e);
}
}
});
}
}
MainActivity
package com.example.administrator.myapplication;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.example.administrator.myapplication.bean.Bean;
import com.example.administrator.myapplication.bean.NewslistBean;
import com.example.administrator.myapplication.presenter.MainPresenter;
import com.example.administrator.myapplication.view.IMaininterface;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity implements IMaininterface{
@BindView(R.id.recyclerview)
RecyclerView recyclerview;
private MyAdapter adapter;
private MainPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
presenter = new MainPresenter(this);
presenter.get();
LinearLayoutManager manager=new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
recyclerview.setLayoutManager(manager);
adapter = new MyAdapter(this);
recyclerview.setAdapter(adapter);
List newslistBeans = IApplication.session.getNewslistBeanDao().loadAll();
for (NewslistBean bean:newslistBeans){
Log.i("=========",bean.toString());
}
//判断网络
if(!isConn(getApplicationContext())){
ConnectionUtil.setNetworkMethod(MainActivity.this);
}
//设置网络判断
public static boolean isConn(Context context){
boolean bisConnFlag=false;
ConnectivityManager conManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo network = conManager.getActiveNetworkInfo();
if(network!=null){
bisConnFlag=conManager.getActiveNetworkInfo().isAvailable();
}
return bisConnFlag;
}
MyAdapter适配器
package com.example.administrator.myapplication;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.administrator.myapplication.bean.Bean;
import com.example.administrator.myapplication.bean.EventBean;
import com.example.administrator.myapplication.bean.NewslistBean;
import com.facebook.drawee.view.SimpleDraweeView;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
* Created by Administrator on 2017/12/1.
*/
class MyAdapter extends RecyclerView.Adapter {
Context context;
List list;
public MyAdapter(Context context) {
this.context = context;
}
public void add(Bean bean) {
if (list == null) {
list = new ArrayList<>();
}
list.addAll(bean.getNewslist());
notifyDataSetChanged();
}
@Override
public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.adapter_item, null);
return new IViewHolder(view);
}
@Override
public void onBindViewHolder(IViewHolder holder, final int position) {
holder.simpledraweeview.setImageURI(list.get(position).getPicUrl());
holder.title.setText(list.get(position).getTitle());
holder.title.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EventBus.getDefault().postSticky(new EventBean(list.get(position).getPicUrl(),list.get(position).getTitle()));
context.startActivity(new Intent(context,Main2Activity.class));
}
});
}
@Override
public int getItemCount() {
return list==null ? 0 : list.size();
}
public class IViewHolder extends RecyclerView.ViewHolder {
@BindView(R.id.simpledraweeview)
SimpleDraweeView simpledraweeview;
@BindView(R.id.title)
TextView title;
public IViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this,itemView);
}
}
}
package com.example.administrator.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.myapplication.bean.EventBean;
import com.facebook.drawee.view.SimpleDraweeView;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import butterknife.BindView;
import butterknife.ButterKnife;
public class Main2Activity extends Activity {
@BindView(R.id.simpledraweeview1)
SimpleDraweeView simpledraweeview1;
@BindView(R.id.title1)
TextView title1;
private String url;
private String title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
title1.setText(title);
simpledraweeview1.setImageURI(url);
}
@Subscribe(sticky = true)
public void event(EventBean eventBean){
url = eventBean.getUrl();
title = eventBean.getTitle();
Toast.makeText(this, " "+eventBean.getUrl()+" "+eventBean.getTitle(), Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
}
ConnectionUtil工具类判断网络
package com.example.administrator.monilianxi;
import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
/**
* Created by Administrator on 2017/12/3.
*/
public class ConnectionUtil {
/*
* 打开设置网络界面
* */
//判断网络
public static void setNetworkMethod(final Context context){
//提示对话框
AlertDialog.Builder builder=new AlertDialog.Builder(context);
builder.setTitle("网络设置提示").setMessage("网络连接不可用,是否进行设置?").setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Intent intent=null;
//判断手机系统的版本 即API大于10 就是3.0或以上版本
if(android.os.Build.VERSION.SDK_INT>10){
intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
}else{
intent = new Intent();
ComponentName component = new ComponentName("com.android.settings","com.android.settings.WirelessSettings");
intent.setComponent(component);
intent.setAction("android.intent.action.VIEW");
}
context.startActivity(intent);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
}).show();
}
}