HttpURLConnection 数据请求+Gson解析,Listview展示

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
    ListView lv;
    List<Data> list = new ArrayList<Data>();
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        lv = (ListView) findViewById(R.id.lv);
        new Thread() {
            @Override
            public void run() {
                gethttpconnection("http://www.yasite.net/shopapi/index.php/goodController/getGoodList/");
            }
        }.start();

    }

    Handler handler = new Handler() {//主线程

        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 2) {
                myadapter adapter = (myadapter) msg.obj;
                lv.setAdapter(adapter);
            }

        }

    };

    public void gethttpconnection(String Url) {
        try {
            URL url = new URL(Url);
            HttpURLConnection hc = (HttpURLConnection) url.openConnection();
            hc.setRequestMethod("GET");//get请求,必须大写
            hc.setReadTimeout(2000);//读取超时
            hc.setConnectTimeout(5000);//请求超时
            if (hc.getResponseCode() == 200) {//正常请求
                InputStream is = hc.getInputStream();
                byte[] b = new byte[is.available()];
                is.read(b);
                String str = new String(b, 0, b.length);
                Gson gson = new Gson();
                Values values = gson.fromJson(str, Values.class);
                list = values.getData();
                myadapter adapter = new myadapter();
                handler.sendMessage(handler.obtainMessage(2, adapter));//发送

            }

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    class myadapter extends BaseAdapter {//Baseadapter

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view = LayoutInflater.from(context).inflate(R.layout.showhttp,
                    null);
            TextView tv = (TextView) view.findViewById(R.id.tv);
            tv.setText(list.get(position).toString());
            return view;
        }

    }

}

 

Gson数据解析实例

package com.example.testhttpconnection_2;

import java.util.List;

public class Values {
    private List<Data> data;
    private int res;

    public Values(List<Data> data, int res) {
        super();
        this.data = data;
        this.res = res;
    }

    public List<Data> getData() {
        return data;
    }

    public void setData(List<Data> data) {
        this.data = data;
    }

    public int getRes() {
        return res;
    }

    public void setRes(int res) {
        this.res = res;
    }
}

 

data实例

package com.example.testhttpconnection_2;

public class Data {
private String goods_id;
private String goods_sn;
private String goods_name;
private String goods_thumb;
private String goods_img;
private String goods_price;
private String shop_price;
public Data(String goods_id, String goods_sn, String goods_name,
        String goods_thumb, String goods_img, String goods_price,
        String shop_price) {
    super();
    this.goods_id = goods_id;
    this.goods_sn = goods_sn;
    this.goods_name = goods_name;
    this.goods_thumb = goods_thumb;
    this.goods_img = goods_img;
    this.goods_price = goods_price;
    this.shop_price = shop_price;
}
public String getGoods_id() {
    return goods_id;
}
public void setGoods_id(String goods_id) {
    this.goods_id = goods_id;
}
public String getGoods_sn() {
    return goods_sn;
}
public void setGoods_sn(String goods_sn) {
    this.goods_sn = goods_sn;
}
public String getGoods_name() {
    return goods_name;
}
public void setGoods_name(String goods_name) {
    this.goods_name = goods_name;
}
public String getGoods_thumb() {
    return goods_thumb;
}
public void setGoods_thumb(String goods_thumb) {
    this.goods_thumb = goods_thumb;
}
public String getGoods_img() {
    return goods_img;
}
public void setGoods_img(String goods_img) {
    this.goods_img = goods_img;
}
public String getGoods_price() {
    return goods_price;
}
public void setGoods_price(String goods_price) {
    this.goods_price = goods_price;
}
public String getShop_price() {
    return shop_price;
}
public void setShop_price(String shop_price) {
    this.shop_price = shop_price;
}
@Override
public String toString() {
    return "Data [goods_id=" + goods_id + ", goods_sn=" + goods_sn
            + ", goods_name=" + goods_name + ", goods_thumb=" + goods_thumb
            + ", goods_img=" + goods_img + ", goods_price=" + goods_price
            + ", shop_price=" + shop_price + "]";
}

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。