private Handler handler = new Handler(){

        @Override

        public void handleMessage(Message msg) {

            if (msg.what == 0){

                DataDataBean dataDataBean = (DataDataBean) msg.obj;

                final List<DataDataBean.ResultBean.DataBean> list = dataDataBean.getResult().getData();

                //设置适配器

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

                listView.setAdapter(myAdapter);

                /**

                 * 条目的点击事件

                 */

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override

                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                        //跳转…..url路径传过去

                        Intent intent = new Intent(MainActivity.this,SecondActivity.class);

                        intent.putExtra(“url”,list.get(i).getUrl());

                        startActivity(intent);

                    }

                });

            }

        }

    };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        listView = (ListView) findViewById(R.id.list_view);

    }

    /**

     * 点击事件中去获取网络上的json数据

     *

     * httpUrlConnection请求的post方式

     *

     * type=top&key=597b4f9dcb50e051fd725a9ec54d6653

     *

     * get方式的地址:http://v.juhe.cn/toutiao/index?type=top&key=597b4f9dcb50e051fd725a9ec54d6653

     */

    public void getDataFromNet(View view){

        //访问之前先判断一下网络状态

        if (NetWorkUtil.isConn(MainActivity.this)){

            new Thread(){

                @Override

                public void run() {

                    //获取数据的地址

                    String path = “http://v.juhe.cn/toutiao/index”;

                    try {

                        URL url = new URL(path);

                        //打开连接

                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                        //设置

                        connection.setRequestMethod(“POST”);

                        connection.setReadTimeout(5000);

                        connection.setConnectTimeout(5000);

                        //设置请求内容的类型

                        connection.setRequestProperty(“Content-Type”,”application/x-www-form-urlencoded”);

                        //设置可以向外输出…因为post方式的时候参数是以流的形式写给服务器,所以设置向外输出

                        connection.setDoOutput(true);

                        //参数

                        String params = “type=top&key=597b4f9dcb50e051fd725a9ec54d6653”;

                        //把参数以流的形式写给服务器

                        connection.getOutputStream().write(params.getBytes());//代码到这个位置,,,参数写给服务器

                        //获取

                        int responseCode = connection.getResponseCode();

                        if (responseCode == 200){

                            //获取到服务器返回的输入字节流….装的是json格式的数据

                            InputStream inputStream = connection.getInputStream();

                            //把字节流转成字符串

                            String json = streamToString(inputStream,”utf-8″);

                            //解析获取到的json格式的字符串

                            Gson gson = new Gson();

                            //生成变量…alt+enter…..此时所有的数据都装到bean里面了

                            DataDataBean dataDataBean = gson.fromJson(json, DataDataBean.class);

                            //由于处在子线程,不能设置适配器,,,所以把bean发送到主线程

                            Message message = Message.obtain();

                            message.what = 0;

                            message.obj = dataDataBean;

                            handler.sendMessage(message);

                        }

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

            }.start();

        }else {

            //没有网络的时候弹窗提示

            NetWorkUtil.showNoNetWorkDlg(MainActivity.this);

        }

    }

    /**

     * 把字节流转换为字符串

     * @param inputStream

     * @param

     * @return

     */

    private String streamToString(InputStream inputStream, String charset) {

        //1.将输入的字节流转为字符流

        try {

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);

            //2.快速的读取需要缓冲流

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String s = null;

            StringBuilder builder = new StringBuilder();//字符串缓冲区

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

                //添加到字符串缓冲区

                builder.append(s);

            }

            bufferedReader.close();

            //返回字符串

            return builder.toString();

        } catch (Exception e) {

            e.printStackTrace();

        }

        return null;

    }


版权声明:本文为qq_40446718原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_40446718/article/details/78191699