异步加载

来源:互联网 发布:超级神基因优化液txt 编辑:程序博客网 时间:2024/05/16 00:30
AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {        @Override        protected String doInBackground(Void... params) {            try {                URL url = new URL("http://v.juhe.cn/toutiao/index?type="+name+"&key=61005cfc63a8075c88d5d408ba90aff9");                HttpURLConnection connection = (HttpURLConnection) url.openConnection();                connection.setRequestMethod("GET");                connection.setReadTimeout(5000);                connection.setConnectTimeout(5000);                int responseCode = connection.getResponseCode();                if (responseCode==200){                    InputStream inputStream = connection.getInputStream();                    String json = getstring(inputStream,"utf-8");                   // Log.d("TAG","+++++++++++++++++"+json);                    return json;                }            } catch (Exception e) {                e.printStackTrace();            }            return null;        }        @Override        protected void onPostExecute(String s) {            Gson gson = new Gson();            Bean bean = gson.fromJson(s, Bean.class);           List<Bean.ResultBean.DataBean> us = bean.getResult().getData();           Myadapter myadapter = new Myadapter(getActivity(), us);          lvv.setAdapter(myadapter);                    }};    asyncTask.execute();//一定要加