Gson解析

public class CuxiaoActivity extends AppCompatActivity {
    private BaseAdapter adapter;
    private ListView lv;
    private Button cu;
    private List<Cuxiaoli> list;

    Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            adapter=new MyAdapter();
            lv.setAdapter(adapter);
        };
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cuxiao);
        //接收listView控件
        lv=(ListView)findViewById(R.id.culv);
        //接收cu控件
        cu=(Button)findViewById(R.id.cu);
        cu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        new Thread(){
            private String text;
            public void run() {
                try {
                    //创建URL
                    URL url=new URL("http://121.42.8.95:8090/ECServer_D/topic");
                    //创建连接HttpURLContention对象
                    HttpURLConnection connection=(HttpURLConnection) url.openConnection();
                    //执行
                    connection.connect();
                    //获得响应吗
                    int code=connection.getResponseCode();
                    //判断响应码正确是为200
                    if(code==200){
                        //输入流
                        InputStream inputStream=connection.getInputStream();
                        byte[] by=new byte[1024];
                        //输出流
                        ByteArrayOutputStream bos=new ByteArrayOutputStream();
                        int len=-1;
                        //
                        while((len=inputStream.read(by))!=-1){
                            bos.write(by, 0, len);
                        }
                        //关闭
                        inputStream.close();
                        //把值付给text
                        text=bos.toString();
                    }
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Gson gson=new Gson();
                Cuxiao cuxi=gson.fromJson(text,Cuxiao.class);
                list=cuxi.getTopic();
                handler.sendEmptyMessageAtTime(0,1000);
            };

        }.start();
    }
    //适配器
    class MyAdapter extends  BaseAdapter{

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

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

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewholder;
            if(convertView!=null){
                viewholder=(ViewHolder) convertView.getTag();
            }
            else{
                convertView= LayoutInflater.from(CuxiaoActivity.this).inflate(R.layout.cuxiaolv,null);
                viewholder=new ViewHolder();
                viewholder.name=(TextView)convertView.findViewById(R.id.cuname);
                viewholder.image=(ImageView)convertView.findViewById(R.id.cuimage);
                convertView.setTag(viewholder);
            }
            viewholder.name.setText(list.get(position).getName());
            ImageLoader imageLoader=ImageLoader.getInstance();
            imageLoader.displayImage(list.get(position).getPic(),viewholder.image);
            return convertView;
        }
        class ViewHolder{
            private TextView name;
            private ImageView image;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值