您的位置:首页 > 移动开发 > Android开发

Android 简介json解析的使用

2015-09-25 15:51 901 查看
首先定义一个http获取数据的方法:

//用于联网获取数据,并将数据转换为String
public 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 block
e.printStackTrace();
}
return result;
}
}


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

本文采用AsyncTask的方式:

private void getdata() {
new AsyncTask<Void, Void, HotProduct>() {

@Override
protected 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
}
@Override
public 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
}
@Override
public String toString() {
return "NewProductList [id=" + id + ", marketprice=" + marketprice
+ ", name=" + name + ", pic=" + pic + ", price=" + price + "]";
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android json url