2016年11月8日火曜日

Android 下载JSON文体,解析为数组,自定义Adapter显示

Android 下载JSON文体,解析为数组,自定义Adapter显示



public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button bt = (Button) findViewById(R.id.button2);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MyasyncTask().execute("http://cdefgab.web.fc2.com/song.json");
            }
        });
    }

//   下载JSON文体,解析为数组,自定义Adapter显示。
    public class MyasyncTask extends AsyncTask<String, Integer, List<String>> {
        @Override
        protected List<String> doInBackground(String... params) {
            InputStream is = null;
            ByteArrayOutputStream baos = null;
            List<String> data = null;
            try {
                URL url = new URL(params[0]);
                HttpURLConnection huc = (HttpURLConnection) url.openConnection();
                huc.setConnectTimeout(3000);
                huc.setReadTimeout(3000);
                huc.setDoInput(true);
                huc.setRequestMethod("GET");
                baos = new ByteArrayOutputStream();
                if (huc.getResponseCode() == 200) {
                    is = huc.getInputStream();
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = is.read(buffer)) != -1) {
                        baos.write(buffer, 0, len);
                    }
                    JSONObject js = new JSONObject(baos.toString());
                    JSONObject js2 = js.getJSONObject("photos");
                    JSONArray ja1 = js2.getJSONArray("photo");
                    data = new ArrayList<String>();
                    for (int i = 0; i < ja1.length(); i++) {
                        JSONObject js3 = ja1.getJSONObject(i);
                        data.add(js3.getString("title"));
                    }
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return data;
        }

        @Override
        protected void onPostExecute(final List<String> strings) {
            super.onPostExecute(strings);
            ListView lv = (ListView) findViewById(R.id.listview);
//            lv.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,strings));
            mya m = new mya(strings);
            m.notifyDataSetChanged();
            lv.setAdapter(m);

            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Toast.makeText(MainActivity.this, strings.get(position), Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public class mya extends BaseAdapter {
        private List<String> temp;

        public mya(List<String> temp) {
            this.temp = temp;
        }

        @Override
        public int getCount() {
            return temp.size();
        }

        @Override
        public Object getItem(int position) {
            return temp.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = null;
            if (convertView == null) {
                view = LayoutInflater.from(MainActivity.this).inflate(R.layout.myadapter1, null);
            } else {
                view = convertView;
            }
            TextView tv = (TextView) view.findViewById(R.id.textView5);
            tv.setText(temp.get(position));
            return view;
        }
    }
}

0 件のコメント: