利用ASIHTTPRequest请求java服务和JSONKit解析返回的json串

来源:互联网 发布:ubuntu升级16.04后黑屏 编辑:程序博客网 时间:2024/06/01 15:09
整ASIHTTPRequest和JSONKit花了我近三个晚上(20点到23点30)的时间,今天终于把后台返回的json串解析出来了,为了模拟真实的调用环境,用spring mvc搭了个简单的java后台服务。提供json数据

运行如下
12.jpg


同步调用、异步调用、块格式调用 说的是用ASIHTTPRequest请求后台的方式。下面有一个按钮是批JSON解析

其实ASIHTTPRequest请求后台都没什么好说的,官方的API说的很清楚,可以看看官方的how to use

时间主要都花在解析后台返回的数据上了。

下面上代码
controller.h文件
  1. #import <UIKit/UIKit.h>
  2. #import "ASIHTTPRequest.h"
  3. #import "JSONKit.h"

  4. @interface ITViewController : UIViewController
  5. @property (strong, nonatomic) IBOutlet UILabel *label;


  6. //同步调用
  7. - (IBAction)buttonPressed:(id)sender;
  8. //异步调用
  9. - (IBAction)asyButtonPressed:(id)sender;
  10. //块格式调用
  11. - (IBAction)blockButtonPressed:(id)sender;
  12. //JSONKit测试
  13. - (IBAction)jsonKitTestButtonPressed:(id)sender;
  14. //JSONKit本地测试
  15. -(IBAction)localJsonKitTest:(id)sender;
  16. //JSONKit返回集合数据(集合中都是原始字段的数据)
  17. - (IBAction)jsonKitArrayListWithYY:(id)sender;
  18. @end
复制代码
ASIHTTPRequest的使用就不写了,官方都有

下面看看调用后台的吧 ,
先贴一下,java端返回的数据:

[
    {
        "name": "北京",
        "id": 1,
        "routes": [
            {
                "id": 1,
                "content": "北京一日游,好玩",
                "days": 1,
                "title": "北京一日游",
                "startDate": 1348070400000,
                "endDate": 1345392000000,
                "scenerySpot": "天安门、长城",
                "price": 168,
                "districtId": 1,
                "bigImgId": 1,
                "samllImgId": 1,
                "specialPrice": 1,
                "routeDate": null,
                "clickCount": null,
                "isRecommend": null
            },
            {
                "id": 2,
                "content": "北京长城 十三陵一日游",
                "days": 1,
                "title": "北京长城 十三陵一日游",
                "startDate": 1345478400000,
                "endDate": 1345478400000,
                "scenerySpot": "长城 十三陵",
                "price": 190,
                "districtId": 1,
                "bigImgId": 1,
                "samllImgId": 1,
                "specialPrice": 1,
                "routeDate": null,
                "clickCount": null,
                "isRecommend": null
            }
        ]
    },
    {
        "name": "上海",
        "id": 2,
        "routes": []
    },
    {
        "name": "湖南",
        "id": 3,
        "routes": []
    },
    {
        "name": "湖北",
        "id": 4,
        "routes": []
    },
    {
        "name": "浙江",
        "id": 5,
        "routes": []
    },
    {
        "name": "江苏",
        "id": 6,
        "routes": []
    },
    {
        "name": "福建",
        "id": 7,
        "routes": []
    },
    {
        "name": "新疆",
        "id": 8,
        "routes": []
    }
]

这个方法是JSONKit远程测试的
  1. //JSONKit测试
  2. - (IBAction)jsonKitTestButtonPressed:(id)sender{
  3.    
  4.     //本地测试json转对象
  5. //    NSString *strJson = @"{\"aps\": {\"alert\":{\"body\":\"a msg come!\"},\"bage\":3,\"sound\":\"def.mp3\"}}";
  6. //    NSDictionary *result = [strJson objectFromJSONString];
  7. //    NSLog(@"%@",result);
  8.    
  9.     //本地测试对象转json
  10.     //生成json数据
  11. //    NSMutableDictionary *jsonDic = [NSMutableDictionary dictionary];
  12. //    NSMutableDictionary *alert = [NSMutableDictionary dictionary];
  13. //    NSMutableDictionary *aps = [NSMutableDictionary dictionary];
  14. //    [alert setObject:@"a msg come!" forKey:@"body"];
  15. //    [aps setObject:alert forKey:@"alert"];
  16. //    [aps setObject:@"3" forKey:@"bage" ];
  17. //    [aps setObject:@"def.mp3" forKey:@"sound"];
  18. //    [jsonDic setObject:aps forKey:@"aps"];
  19. //    NSString *strJson = [jsonDic JSONString];
  20. //    NSLog(@"%@",strJson);
  21.    
  22.     //远程测试读取java服务中返回的json
  23.     //构造一个url http://192.168.0.92:8080/ly/w/route/ios_test
  24.     //http://192.168.0.92:8080/ly/w/route/findRouteByCategoryId?categoryId=1
  25.     NSURL *url = [NSURL URLWithString:@"http://192.168.0.92:8080/ly/w/route/findRouteByCategoryId?categoryId=1"];
  26.    
  27.     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  28.     [request startSynchronous];
  29.    
  30.     NSError *error = [request error];
  31.     if (!error) {
  32.         NSString *response = [request responseString];
  33.         
  34.         NSLog(@"请求的结果是;%@",response);
  35.         label.text = @"succeeded";
  36.         
  37.         //用[obj class]查看后发现返回的数据是JKArray
  38.         NSArray *result = [response objectFromJSONString];
  39.         NSLog(@"%@",[result class]);
  40.         
  41.         NSLog(@"%@",[[result objectAtIndex:0] class]);
  42.         
  43.         for (int i = 0; i < [result count]; i++) {
  44.             //取得result的每一条记录
  45.             NSDictionary *di1 = [result objectAtIndex:i];
  46.             NSString *t_id = [di1 objectForKey:@"id"];
  47.             NSString *t_name = [di1 objectForKey:@"name"];
  48.             NSLog(@"第%d条记录的id值是%@",i+1,t_id);
  49.             NSLog(@"第%d条记录的name值是%@",i+1,t_name);
  50.             
  51.             //取得每个城市下的线路routes
  52.             NSArray *routes = [di1 objectForKey:@"routes"];
  53.             //循环线路
  54.             for (NSDictionary *route in routes) {
  55.                 //取得每个线路下的标题
  56.                 NSString *title = [route objectForKey:@"title"];
  57.                 NSLog(@"%@",title);
  58.             }
  59.             
  60.         }
  61.         
  62.         
  63.     }
  64. }
复制代码
先用NSUrl构造一个url,然后用ASIHTTPRequest请求,如果没错误的情况下用NSString来接收返回的值
  1. NSString *response = [request responseString];
复制代码
调用JSONKit转成NSArray
  1. [response objectFromJSONString];
复制代码
时间就是花在这了,当时不知道这个地方到底该用什么接收,网上查都说是用NSDictionary,但是用这个就一直报错,后面听群里的网友说,可以用[obj class]来确定是什么类型,试了下,果真好使,,,,到这基本上就没什么问题了 ,哈哈。
当用[obj class]得知返回的数据是Array之后,就直接用NSArray接收就好了,后面什么都通顺了。。。

点击JSONKit远程测试,控制台输出:
13.jpg
记录一下,当时纠结的心情只有自己知道。

路漫漫其修远兮,吾将上下而求索。
原创粉丝点击