• android发送/接收json数据(转)


    客户端向服务器端发送数据,这里用到了两种,一种是在url中带参数,一种是json数据发送方式;

    url带参数的写法:

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

     

     

    从“&”符号之后一连串都是参数。

    发送方式代码编写"

    Java代码 复制代码 收藏代码
    1. DefaultHttpClient httpClient = new DefaultHttpClient();   
    2.         HttpPost httpPost = new HttpPost(HttpUtil.BASIC_URL   
    3.                 + HttpUtil.SUBSCRIPTION_URL);   
    4.         try{   
    5.             if (cookie != null) {   
    6.                // httpClient.setCookieStore(LoginJsonUtil.cookie);   
    7.                 List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);   
    8.                 nameValuePair.add(new BasicNameValuePair("uid",   
    9.                         uid));   
    10.                 nameValuePair.add(new BasicNameValuePair("subscriptionslist[pageindex]",   
    11.                         subscriptionslist_pageindex));   
    12.                 nameValuePair.add(new BasicNameValuePair("subscriptionslist[recordlimit]",   
    13.                         subscriptionslist_recordlimit));   
    14.                 httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));  
    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));
                    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
     

    传递参数使用nameValuePair。

    如果使用cookie的话,使用上段代码中注释掉的部分

    httpClient.setCookieStore(LoginJsonUtil.cookie);

     

    使用json数据格式发送信息向服务器端:

     

    Java代码 复制代码 收藏代码
    1. HttpClient httpClient = new DefaultHttpClient();   
    2.         try {   
    3.             HttpPost httpPost = new HttpPost(BASIC_URL + url);   
    4.             List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();   
    5.             JSONObject jsonObject = new JSONObject();   
    6.             JSONObject jsonObject2 = new JSONObject();   
    7.             jsonObject.put("uemail", userbean.getEmail());   
    8.             jsonObject.put("password", userbean.getPassword());   
    9.             jsonObject2.put("userbean", jsonObject);   
    10.             nameValuePair.add(new BasicNameValuePair("jsonString", jsonObject   
    11.                     .toString()));   
    12.             Log.i("lifeweeker", jsonObject2.toString());   
    13.             httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));  
    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}}

    分析代码如下:

     

    Java代码 复制代码 收藏代码
    1. // TODO 状态处理 500 200   
    2.                 int res = 0;   
    3.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();   
    4.                 if (res == 200) {   
    5.                       
    6.                     HttpResponse httpResponse = httpClient.execute(httpPost);   
    7.                     StringBuilder builder = new StringBuilder();   
    8.                     BufferedReader bufferedReader2 = new BufferedReader(   
    9.                             new InputStreamReader(httpResponse.getEntity().getContent()));   
    10.                     String str2 = "";   
    11.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2   
    12.                             .readLine()) {   
    13.                         builder.append(s);   
    14.                     }   
    15.                     Log.i("cat"">>>>>>" + builder.toString());   
    16. JSONObject jsonObject = new JSONObject(builder.toString())   
    17.                         .getJSONObject("userbean");   
    18.                 String Uid;   
    19.                 String Showname;   
    20.                 String Avtar;   
    21.                 String State;   
    22.                 Uid = jsonObject.getString("Uid");   
    23.                 Showname = jsonObject.getString("Showname");   
    24.                 Avtar = jsonObject.getString("Avtar");   
    25.                 State = jsonObject.getString("State");  
    // 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");
     

    带数组形式的:

    服务器端返回的数据格式为:

     

    Java代码 复制代码 收藏代码
    1. {"calendar":   
    2.     {"calendarlist":   
    3.             [   
    4.            {"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},   
    5.            {"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}   
    6.             ]   
    7.     }   
    8. }   
    9. 分析代码如下:   
    10. // TODO 状态处理 500 200   
    11.                 int res = 0;   
    12.                 res = httpClient.execute(httpPost).getStatusLine().getStatusCode();   
    13.                 if (res == 200) {   
    14.                       
    15.                     HttpResponse httpResponse = httpClient.execute(httpPost);   
    16.                     StringBuilder builder = new StringBuilder();   
    17.                     BufferedReader bufferedReader2 = new BufferedReader(   
    18.                             new InputStreamReader(httpResponse.getEntity().getContent()));   
    19.                     String str2 = "";   
    20.                     for (String s = bufferedReader2.readLine(); s != null; s = bufferedReader2   
    21.                             .readLine()) {   
    22.                         builder.append(s);   
    23.                     }   
    24.                     Log.i("cat"">>>>>>" + builder.toString());   
    25.                       
    26.                     JSONObject jsonObject = new JSONObject(builder.toString())   
    27.                             .getJSONObject("calendar");   
    28.                     JSONArray jsonArray = jsonObject.getJSONArray("calendarlist");   
    29.                     for(int i=0;i<jsonArray.length();i++){   
    30.                         JSONObject jsonObject2 = (JSONObject)jsonArray.opt(i);   
    31.                         CalendarInfo calendarInfo = new CalendarInfo();   
    32.                         calendarInfo.setCalendar_id(jsonObject2.getString("calendar_id"));   
    33.                         calendarInfo.setTitle(jsonObject2.getString("title"));   
    34.                         calendarInfo.setCategory_name(jsonObject2.getString("category_name"));   
    35.                         calendarInfo.setShowtime(jsonObject2.getString("showtime"));   
    36.                         calendarInfo.setEndtime(jsonObject2.getString("endshowtime"));   
    37.                         calendarInfo.setAllDay(jsonObject2.getBoolean("allDay"));   
    38.                         calendarInfos.add(calendarInfo);   
    39.                     }  
    {"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。

  • 相关阅读:
    Registering a Visual Studio .Net AddIn without an Installer
    广州的.Net Fans俱乐部的T恤和Logo的设计
    模板控件概念(转)
    随笔-在广州.Net俱乐部的一个下午
    快速备份Outlook中的资料
    web.config文件详解
    使用RAPI库操作移动设备——C#语言描述 (转贴)
    Ready to Rock 撼动未来:SQL Server 2005 ,Visual Studio 2005, BizTalk Server 2006 新品发布会 报名
    IBM Rational Solutions V2003
    VS.Net 2003 IDE 插件开发指南
  • 原文地址:https://www.cnblogs.com/lsysunbow/p/2560193.html
Copyright © 2020-2023  润新知