Android 简介json解析的使用

来源:互联网 发布:贝尔地板 知乎 编辑:程序博客网 时间:2024/05/21 19:33

首先定义一个http获取数据的方法:

//用于联网获取数据,并将数据转换为Stringpublic class HttpUtil {public static String get(String url) { String result = "";HttpClient client = new DefaultHttpClient();HttpGet get=new HttpGet(url);HttpResponse response;try {response = client.execute(get);if(200==response.getStatusLine().getStatusCode()){InputStream in=response.getEntity().getContent();ByteArrayOutputStream baos=new ByteArrayOutputStream();byte[] buffer=new byte[1024];int len;while((len=in.read(buffer))!=-1){baos.write(buffer, 0, len);}baos.flush();in.close();baos.close();byte[] bt=baos.toByteArray();result=new String(bt,"utf-8");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return result;}}

然后创建一个子线程去获取数据,可以使用Thread+handler的方式操作UI,也可以使用AsyncTask进行处理.

本文采用AsyncTask的方式:

private void getdata() {new AsyncTask<Void, Void, HotProduct>() {@Overrideprotected HotProduct doInBackground(Void... params) {String json = HttpUtil.get(url);  //联网获取数据Gson gson = new Gson();  //创建Gson对象,用于json解析,需要gson.jar包HotProduct hotProduct = gson.fromJson(json, HotProduct.class);   //此处用定义的格式去解析,并将数据赋予相应的对象return hotProduct;}}.execute();}

联网获取到的数据如下:

{"productlist":[{"id":10000,"marketprice":1000,"name":"雅戈尔0","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10001,"marketprice":1001,"name":"雅1","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10002,"marketprice":1002,"name":"雅2","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10003,"marketprice":1003,"name":"雅3","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10004,"marketprice":1004,"name":"雅4","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10005,"marketprice":1005,"name":"雅5","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10006,"marketprice":1006,"name":"雅6","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10007,"marketprice":1007,"name":"雅7","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10008,"marketprice":1008,"name":"雅8","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10009,"marketprice":1009,"name":"雅9","pic":"http://192.168.1.105:8080/EServer_D/images/image10.png","price":800}],"response":"hotproduct","list_count":10}

HotProduct类如下定义:

public class HotProduct {public ArrayList<NewProductList> productlist;public String response;public String list_count;public HotProduct() {super();// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "HotProduct [productlist=" + productlist + ", response="+ response + ", list_count=" + list_count + "]";}}

NewProductList:
public class NewProductList {public String id;public String marketprice;public String name;public String pic;public String price;public NewProductList() {super();// TODO Auto-generated constructor stub}@Overridepublic String toString() {return "NewProductList [id=" + id + ", marketprice=" + marketprice+ ", name=" + name + ", pic=" + pic + ", price=" + price + "]";}}



0 0
原创粉丝点击