解析json获取网络数据

public class SecondActivity extends AppCompatActivity {

    private ViewPager view_pager;
    private GridView grid_view;
    private String uri = "http://172.17.8.100/mobile/exam/findCommodityList";
    private String data;
    private List plist;
    private List newsList;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                int i = view_pager.getCurrentItem();
                i++;
                view_pager.setCurrentItem(i);
                handler.sendEmptyMessageDelayed(0, 2000);
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //初始化控件
        view_pager = findViewById(R.id.view_pager);
        grid_view = findViewById(R.id.grid_view);
        new AsyncTask().execute();
        grid_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                Toast.makeText(SecondActivity.this,newsList.get(position).getName() , Toast.LENGTH_SHORT).show();
            }
        });
    }

    class AsyncTask extends android.os.AsyncTask {



        @Override
        protected String doInBackground(Integer... integers) {

            try {
                URL url = new URL(uri);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setReadTimeout(5000);
                connection.setConnectTimeout(5000);
                connection.connect();
                int code = connection.getResponseCode();
                if (code == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                    data = ToString(inputStream);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return data;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            Gson gson = new Gson();
            News news = gson.fromJson(data, News.class);
            plist = news.getResult().getPics();//图片集合
            newsList = news.getResult().getProduct();
            MyPaMyPagerAdapter pagerAdapter = new MyPaMyPagerAdapter();
            view_pager.setAdapter(pagerAdapter);
            handler.sendEmptyMessageDelayed(0, 2000);
            MyAdapter adapter = new MyAdapter(SecondActivity.this, newsList);
            grid_view.setAdapter(adapter);
        }

        class MyPaMyPagerAdapter extends PagerAdapter {

            @Override
            public int getCount() {
                return Integer.MAX_VALUE;
            }

            @Override
            public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
                return view == o;
            }

            @NonNull
            @Override
            public Object instantiateItem(@NonNull ViewGroup container, final int position) {
                ImageView imageView = new ImageView(SecondActivity.this);
                ImageLoader.getInstance().displayImage(plist.get(position % plist.size()).getPic(), imageView);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(SecondActivity.this,WebActivity.class);
                        intent.putExtra("url",plist.get(position%plist.size()).getUrl());
                        startActivity(intent);
                    }
                });
                container.addView(imageView);
                return imageView;
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                container.removeView((View) object);
            }
        }
    }

    private String ToString(InputStream inputStream) {

        try {
            InputStreamReader reader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(reader);
            StringBuilder builder = new StringBuilder();
            String str = "";
            while ((str = bufferedReader.readLine()) != null) {
                builder.append(str);
            }
            bufferedReader.close();
            return builder.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}

 

你可能感兴趣的:(解析json获取网络数据)