Android 网络判断+接口请求+添加DB+DB查询

工具类必不可少

//网络请求方法
 public  static  String getJson(String urlString){

        try {
            //封装接口
            URL url = new URL(urlString);

            //打开连接
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();

            int responseCode = urlConnection.getResponseCode();

            if (responseCode == 200){

                InputStream inputStream = urlConnection.getInputStream();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

                String temp="";

                String str="";

                while ((temp = bufferedReader.readLine())!=null){

                    str+=temp;
                }
                return str;

            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return " ";
    }
//判断网络状态
    public static boolean isConn(Context context){

        boolean kk = false;

        ConnectivityManager systemService = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo network = systemService.getActiveNetworkInfo();

        if (network!=null){

            kk = network.isAvailable();
        }

        return kk;

    };
}

第一个页面:有网状态下 请求接口数据 Gson解析 并LV展示

public class Frag01 extends Fragment {

    public String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
    private ListView listView;
    private List data = new ArrayList();
    private Dao dao;
    private MyListAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.frag01,container,false);

        listView = view.findViewById(R.id.list);
        adapter = new MyListAdapter(data, getActivity());
        listView.setAdapter(adapter);

//判断是否有网
        if (NetWork.isConn(getActivity())){
//异步方法
            new myAsyncTask().execute(urlString);

        }else{

            Toast.makeText(getActivity(),"没有网络哦",Toast.LENGTH_SHORT).show();

        }

        dao = new Dao(getActivity());

        return  view;
    }

    class  myAsyncTask extends AsyncTask{


        private long insert;

        @Override
        protected String doInBackground(String... strings) {

            String json = NetWork.getJson(urlString);

            return json;
        }

        @Override
        protected void onPostExecute(String s) {

            Gson gson = new Gson();

            GsonBean bean = gson.fromJson(s, GsonBean.class);

            data.addAll(bean.getData());
            adapter.notifyDataSetChanged();;


            //添加数据库
            for (int i = 0 ; i 

第二个页面 无网 查询DB LV展示

public class Frag02 extends Fragment {

    private Dao dao;
    private ListView listView;
    private ArrayList  list = new ArrayList();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        View view = inflater.inflate(R.layout.frag02,container,false);

        listView = view.findViewById(R.id.list1);

        //查询db中的数据

        if (!NetWork.isConn(getActivity())){

            dao = new Dao(getActivity());

            Cursor query = dao.query("kk", null, null, null, null, null, null, null);

            if (query.moveToFirst()){

                do {
                    String news_title = query.getString(query.getColumnIndex("news_title"));
                    String news_summary = query.getString(query.getColumnIndex("news_summary"));
                    String pic_url = query.getString(query.getColumnIndex("pic_url"));

                    list.add(new GsonBean.DataBean("",news_title,news_summary,pic_url));

                }while (query.moveToNext());


            }

            query.close();
        }else{

            Toast.makeText(getActivity(),"有网络哦",Toast.LENGTH_SHORT).show();


        }



        listView.setAdapter(new MyListAdapter1(list,getActivity()));

        return view;
    }
}

日渐努力而后风生水起

你可能感兴趣的:(Android 网络判断+接口请求+添加DB+DB查询)