解析添加数据库

package com.example.lianxi_yue;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

import static android.R.id.content;
import static android.R.id.list;

public class MainActivity extends AppCompatActivity {

    private Caozuo caozuo;
    private ListView lv;

    //判断网络
    private boolean chexkNet(){
        ConnectivityManager conn= (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info =   conn.getActiveNetworkInfo();
        if(info!=null&&info.isConnected()){
            return true;
        }

        return false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        caozuo = new Caozuo(MainActivity.this);
        //判断网络
        boolean b=chexkNet();
        if(!b){
            //适配器
            Youhua2 youhua2 = new Youhua2(MainActivity.this,caozuo.chaxunquan());
            lv.setAdapter(youhua2);

            AlertDialog.Builder builder=new AlertDialog.Builder(this);
            builder.setMessage("去设置");
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this,"去设置网络",Toast.LENGTH_SHORT).show();
                    Intent it=new Intent("android.settings.WIRELESS_SETTINGS");
                    startActivity(it);
                    return;

                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(MainActivity.this,"查看本地",Toast.LENGTH_SHORT).show();

                }
            });
            builder.create();
            builder.show();

        }
        else{
            Toast.makeText(MainActivity.this,"网络连接",Toast.LENGTH_SHORT).show();;
            getdata("http://v.juhe.cn/toutiao/index?type=%22%20+%20android.R.attr.name%20+%20%22&key=54e3d5f4ee64f51bef570ce8505d37b5");
        }

        //操作数据库

//        String chaxunquan = caozuo.chaxunquan();
//
//        Toast.makeText(MainActivity.this,"==="+chaxunquan,Toast.LENGTH_SHORT).show();

    }
    public  void getdata(final String data){
        new AsyncTask<String,Void,String>(){
            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Gson gson  = new Gson();
                Bean bean = gson.fromJson(s, Bean.class);
                //String list = bean.getResult().getData().get(0).getTitle();
                List<Bean.ResultBean.DataBean> data1 = bean.getResult().getData();

                for (Bean.ResultBean.DataBean dataBean : data1) {
                    caozuo.tianjia(dataBean.getTitle());
                }

                //适配器
                Youhua youhua = new Youhua(MainActivity.this,data1);
                lv.setAdapter(youhua);
                //添加数据库


                System.out.println("============="+ android.R.id.list);
            }

            @Override
            protected String doInBackground(String... strings) {
                String url = strings[0];
                try {
                    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    int code = connection.getResponseCode();
                    if (code==200){
                        InputStream is = connection.getInputStream();
                        String shuju = Jiexi.shuju(is);
                        return shuju;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(data);
    }



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值