RetrofitUtils的工具类

public class RetrofitUtils {

    private static RetrofitUtils retrofitUtils;

    private RetrofitUtils() {
    }

    public static RetrofitUtils getInstance() {

        if (retrofitUtils == null) {
            synchronized (RetrofitUtils.class) {
                if (retrofitUtils == null) {
                    retrofitUtils = new RetrofitUtils();
                }
            }
        }
        return retrofitUtils;
    }

    //返回Retrofit
    public static Retrofit getRetrofit(String url) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        return retrofit;
    }

    public <T> T getApiService(String url, Class<T> service) {

            Retrofit retrofit = getRetrofit(url);
            //通过java动态代理模式获取代理对象
            T t = retrofit.create(service);
            return t;

    }
}


public class Api {
    public static  final String baseUrl = "http://www.xieast.com/api/news/";
}


public interface ApiService {

    //news.php?page=1
    @GET("news.php")
    Call<NewsInfo> getNews(@Query("page") int page);
}

public class MainActivity extends AppCompatActivity {

    private SwipeRefreshLayout srl;
    private RecyclerView rv;
    private int page = 1;
    private DataInfoDao dataInfoDao;
    private Handler handler = new Handler();
    private boolean netWork;
    private List<DataInfo> list = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //得到
        dataInfoDao = MyApplication.getInstance().getDaoSession().getDataInfoDao();


        //找控件
        initView();

        //判断网络
        netWork = NetWorkUtils.getNetWork(this);

        if (netWork) {
            //获取网络数据
            getServerData(page);
        } else {
            //走缓存
            List<DataInfo> dataInfos = dataInfoDao.loadAll();
            if (dataInfos.size() == 0) {
                Toast.makeText(this, "无缓存", Toast.LENGTH_SHORT).show();
            } else {
                //获取缓存数据级列表适配器
                Toast.makeText(this, "缓存", Toast.LENGTH_SHORT).show();

                MyAdapter adapter = new MyAdapter(MainActivity.this, dataInfos);

                rv.setAdapter(adapter);
            }

        }


    }


    private void initView() {
        srl = findViewById(R.id.srl);
        rv = findViewById(R.id.rv);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv.setLayoutManager(linearLayoutManager);


        //设置下拉时圆圈的颜色(可以尤多种颜色拼成)
        srl.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light,
                android.R.color.holo_orange_light);
        //设置下拉时圆圈的背景颜色(这里设置成白色)
        srl.setProgressBackgroundColorSchemeResource(android.R.color.white);


        //下拉
        srl.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {


                if (netWork) {
                    page = 1;
                    //获取网络数据
                    getServerData(page);
                } else {
                    Toast.makeText(MainActivity.this, "暂时无网络", Toast.LENGTH_SHORT).show();
                }
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        srl.setRefreshing(false);
                    }
                }, 2000);
            }
        });

        //上拉

        rv.addOnScrollListener(new OnLoadMoreListener() {
            @Override
            protected void onLoading(int countItem, int lastItem) {
                if (netWork) {
                    page++;
                    //获取网络数据
                    getServerData(page);
                } else {
                    Toast.makeText(MainActivity.this, "暂时无网络", Toast.LENGTH_SHORT).show();
                }
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        srl.setRefreshing(false);
                    }
                }, 2000);
            }
        });
    }


    private void getServerData(final int page) {
        //封装
        ApiService apiService = RetrofitUtils.getInstance().getApiService(Api.baseUrl, ApiService.class);
        Call<NewsInfo> call = apiService.getNews(page);
        call.enqueue(new Callback<NewsInfo>() {
            @Override
            public void onResponse(Call<NewsInfo> call, Response<NewsInfo> response) {
                NewsInfo newsInfo = response.body();
                List<DataInfo> data = newsInfo.getData();

                //同时做缓存
                if (page == 1) {
                    dataInfoDao.deleteAll();

                    for (int i = 0; i < data.size(); i++) {
                        DataInfo dataInfo = data.get(i);
                        dataInfoDao.insert(dataInfo);
                    }

                    Toast.makeText(MainActivity.this, "缓存成功", Toast.LENGTH_SHORT).show();
                    //page为1的时候创建集合
                    list = new ArrayList<>();


                }
              //累加数据
                list.addAll(data);

                Toast.makeText(MainActivity.this, "长度:"+list.size(), Toast.LENGTH_SHORT).show();


                //一把数据给列表适配器

                MyAdapter adapter = new MyAdapter(MainActivity.this, list);

                rv.setAdapter(adapter);

                //处理新数据展示位置
                rv.scrollToPosition(adapter.getItemCount()-data.size()-1);

            }

            @Override
            public void onFailure(Call<NewsInfo> call, Throwable t) {

            }
        });

    }
}




©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值