public class fragment1 extends Fragment { private String url="http://mock.eoapi.cn/success/cjTiF8H7R6Dw4fmRsuUS3H7ZPaVUJzRW"; private MyaDapter adapter; private ListView lv; private ArrayList<BodyBean> bodyBeens; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.f1layout, null); lv = (ListView) view.findViewById(R.id.listview1); bodyBeens = new ArrayList<>(); if (adapter == null) { adapter = new MyaDapter(getContext(), bodyBeens); } lv.setAdapter(adapter); getinternetData(); return view; } private void getinternetData() { mAsyncTask mt=new mAsyncTask(); mt.execute(url); } class mAsyncTask extends AsyncTask<String,Integer,String>{ @Override protected String doInBackground(String... strings) { String u = strings[0]; HttpClient client=new DefaultHttpClient(); HttpGet httpGet=new HttpGet(u); try { HttpResponse response = client.execute(httpGet); HttpEntity entity = response.getEntity(); String s = EntityUtils.toString(entity); return s; } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson=new Gson(); Bean bean = gson.fromJson(s, Bean.class); bodyBeens .addAll((ArrayList<BodyBean>) bean.getData().get(1).getBody().get(0)); //adapter = new MyaDapter(getContext(),bodyBeens); adapter.notifyDataSetChanged(); } @Override protected void onProgressUpdate(Integer... values) { super.onProgressUpdate(values); } }
FRAGMENT
最新推荐文章于 2022-10-15 20:35:19 发布