AsyncHttpClient 网络请求+ fastJson解析数据

来源:互联网 发布:黑莓 转制软件 编辑:程序博客网 时间:2024/06/05 21:58

1.首先需要在代码中添加这两个控件的jar包支持:
这里写图片描述
厚颜无耻附上下载链接:
http://download.csdn.net/download/sk2die/10153192
2.使用get请求获取服务器数据(json)
注意:json解析时应判断json数据格式是否正确书写,否则容易出错。
json数据格式:

{    "imageArr": [        {            "ID": "1",            "IMAPAURL": "http:// ******** ",            "IMAURL": "http:// ********"        },        {            "ID": "2",            "http:// ********",            "IMAURL": "http:// ********"        },        {            "ID": "3",            "IMAPAURL": "http:// ********",            "IMAURL": "http:// ********"        },        {            "ID": "5",            "IMAPAURL": "http:http:// ********",            "IMAURL": "http:// ********"        }    ],    "proInfo": {        "id": "1",        "memberNum": "100",        "minMoney": "100",        "money": "10",       }}

对应的类如下(相应的Image和product又是一个类,这里就不相应贴出,原理相同):

public class Index {    public Product product;    public List<Image> imageList;}
AsyncHttpClient client = new AsyncHttpClient();client.get("这里应放上面的json数据的url地址", new AsyncHttpResponseHandler() {    @Override    public void onSuccess(int statusCode, String content) {        index = new Index();        JSONObject jsonObject = JSON.parseObject(content);        String imageArr = jsonObject.getString("imageArr");        List<Image> images = JSON.parseArray(imageArr, Image.class);        String proInfo = jsonObject.getString("proInfo");        Product product = JSON.parseObject(proInfo, Product.class);        index.product = product;        index.imageList = images;    }    @Override    public void onFailure(Throwable error, String content) {        super.onFailure(error, content);    }});

如此,json解析之后index对象中就存放了前面json数据格式中的全部数据,可以通过点属性(如index.product.id)的方式获取。
3.利用post请求向服务器传递json数组:

下面是json数组的封装:
下面的object仅有两个属性,id和answer。个数为mapSize个。

JSONArray jsonArray = new JSONArray();for (int i = 0; i < mapSize; i++) {    JSONObject jsonObject = new JSONObject();    jsonObject.put("id", i);    jsonObject.put("answer", et[i].getText().toString());    jsonArray.add(jsonObject);}

AsyncHttpClient的处理部分

AsyncHttpClient client = new AsyncHttpClient();String url = AppHttpUrl.url;client.addHeader("cookie", Util.getSessionId());//添加cookie,表示请求头。RequestParams params = new RequestParams();params.put("ans",jsonArray.toString());client.post(getApplicationContext(), url,params,new AsyncHttpResponseHandler() {    @Override    public void onSuccess(int statusCode, String content) {        super.onSuccess(statusCode, content);        JSONObject jsonObject = JSON.parseObject(content);        if (jsonObject != null) {            if (jsonObject.getInteger("success") == 0) {                Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_SHORT).show();                 AppManager.getInstance().removeAll();            }          }    }    @Override    public void onFailure(Throwable error, String content) {        super.onFailure(error, content);    }});

将session保存到SharedPreferences中

Headers headers = response.headers();List<String> cookies = headers.values("Set-Cookie");    if (cookies.size() > 0) {        String session = cookies.get(0);        String s = session.substring(0, session.indexOf(";"));        Util.setPhpSessionId(s);     }
原创粉丝点击