android發送/接收json數據

url帶參數的寫法:

url+/?r=m/calendar/contact_list&uid=3&&subscriptionslist[pageindex]=10&subscriptionslist[recordlimit]=10android

 

從「&」符號以後一連串都是參數。web

發送方式代碼編寫:json

DefaultHttpClient httpClient = new DefaultHttpClient();數組

        HttpPost httpPost = new HttpPost(HttpUtil.BASIC_URL
                + HttpUtil.SUBSCRIPTION_URL);
        try{
            if (cookie != null) {
               // httpClient.setCookieStore(LoginJsonUtil.cookie);
                List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);服務器

                nameValuePair.add(new BasicNameValuePair("uid",
                        uid));
                nameValuePair.add(new BasicNameValuePair("subscriptionslist[pageindex]",
                        subscriptionslist_pageindex));
                nameValuePair.add(new BasicNameValuePair("subscriptionslist[recordlimit]",
                        subscriptionslist_recordlimit));cookie

                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));app

傳遞參數使用nameValuePair。ui

若是使用cookie的話,使用上段代碼中註釋掉的部分url

httpClient.setCookieStore(LoginJsonUtil.cookie);
spa

 

使用json數據格式發送信息向服務器端:

HttpClient httpClient = new DefaultHttpClient();
        try {

            HttpPost httpPost = new HttpPost(BASIC_URL + url);

            List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
            JSONObject jsonObject = new JSONObject();
            JSONObject jsonObject2 = new JSONObject();

            jsonObject.put("uemail", userbean.getEmail());
            jsonObject.put("password", userbean.getPassword());

            jsonObject2.put("userbean", jsonObject);

            nameValuePair.add(new BasicNameValuePair("jsonString", jsonObject
                    .toString()));

            Log.i("lifeweeker", jsonObject2.toString());
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));

這裏每一個put的順序我不清楚有沒有規定,我是嚴格按照提供的先後順序來組合json數據格式的。



前面我有用到android發送json數據;這裏我想總結一下我用到的解析json數據格式的方式

json數據格式解析我本身分爲兩種;

一種是普通的,一種是帶有數組形式的;

 

普通形式的:

服務器端返回的json數據格式以下:

{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}

分析代碼以下:

// TODO 狀態處理 500 200
                int res = 0;
                res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
                if (res == 200) {
                   
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    StringBuilder builder = new StringBuilder();
                    BufferedReader bufferedReader2 = new BufferedReader(
                            new InputStreamReader(httpResponse.getEntity().getContent()));
                    String str2 = "";
                    for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
                            .readLine()) {
                        builder.append(s);
                    }
                    Log.i("cat", ">>>>>>" + builder.toString());

JSONObject jsonObject = new JSONObject(builder.toString())
                        .getJSONObject("userbean");

                String Uid;
                String Showname;
                String Avtar;
                String State;

                Uid = jsonObject.getString("Uid");
                Showname = jsonObject.getString("Showname");
                Avtar = jsonObject.getString("Avtar");
                State = jsonObject.getString("State");

帶數組形式的:

服務器端返回的數據格式爲:

{"calendar":
    {"calendarlist":
            [
            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
            ]
    }
}

分析代碼以下:

// TODO 狀態處理 500 200
                int res = 0;
                res = httpClient.execute(httpPost).getStatusLine().getStatusCode();
                if (res == 200) {
                   
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    StringBuilder builder = new StringBuilder();
                    BufferedReader bufferedReader2 = new BufferedReader(
                            new InputStreamReader(httpResponse.getEntity().getContent()));
                    String str2 = "";
                    for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2
                            .readLine()) {
                        builder.append(s);
                    }
                    Log.i("cat", ">>>>>>" + builder.toString());
                   
                    JSONObject jsonObject = new JSONObject(builder.toString())
                            .getJSONObject("calendar");
                    JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");
                    for(int i=0;i<jsonArray.length();i++){
                        JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);
                        CalendarInfo calendarInfo = new CalendarInfo();
                        calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));
                        calendarInfo.setTitle(jsonObject2.getString("title"));
                        calendarInfo.setCategory_name(jsonObject2.getString("category_name"));
                        calendarInfo.setShowtime(jsonObject2.getString("showtime"));
                        calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));
                        calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));
                        calendarInfos.add(calendarInfo);
                    }

總結,普通形式的只需用JSONObject ,帶數組形式的須要使用JSONArray 將其變成一個list。

相關文章
相關標籤/搜索