afn2.0同步和异步 及读取json格式

写json格式时需注意

用NSString写json时,每一个引号前面要加“\”


异步代码

-(void)afHTTPGetData:(NSString *)url { //这个方法用AFNetworking 框架

    NSLog(@"%@",url);

    NSLog(@"afnetworking request.");

    NSURL *httpurl = [[NSURL alloc] initWithString:url];

    NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:httpurl cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20];

    

    NSString *str1 = @"{\"biz\":{\"key\":\"-1\",\"pageSize\":\"10\",\"pageStartRow\":\"0\"},\"sys\":{\"appcode\":\"moa\",\"compressdata\":false,\"encryptdata\":\"true\",\"keycode\":\"5yvfrapr8552006067505393265302\",\"method\":\"report\"}}";

    NSMutableDictionary *dict1=[NSMutableDictionary dictionary];

    [dict1 setObject:str1 forKey:@"xmas-json"];

    

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];

    

    

     NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:httpurl];

   

    

    [manager POST:url parameters:dict1 success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

        NSString *jsonString = jsonData[@"dataString"];

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]

                                                            options:NSJSONReadingMutableLeaves

                                                              error:nil];

        NSArray *resourceArray = dic[@"resultResourceList"];

        NSLog(@"123%@",resourceArray);

   

    

        NSMutableArray *array = [NSMutableArray array];

        for (NSArray *arr in resourceArray)

        {

            for (NSDictionary *arr1 in arr)

            {

                 NSString *chargrUnit =[arr1 objectForKey:@"commodityName"];

                NSLog(@"req responseObject %@",chargrUnit);


          

       

            

            NSString *photoURlString=[NSString stringWithFormat:@"http://go.bgzy.com /webdocs/baozy/upload_image_mobile_size1/%@",[arr1 objectForKey:@"img_Path"]];

            [Marr1 addObject:[ NSURL URLWithString:photoURlString]];

            NSLog(@"req responseObject %@",photoURlString);

            

            }

        }

    

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        if (error) {

                        NSLog(@"req error %@",error);

                   }

    }];

    

    

    

    

}


同步代码

-(void)afHTTPGetData123:(NSString *)url {

    //NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

    BOOL isTrue = false;

   

    NSURL *url1 = [NSURL URLWithString:url];

    NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url1];

    urlrequest.HTTPMethod = @"POST";

    NSString *bodyStr1 = @"{\"biz\":{\"key\":\"-1\",\"pageSize\":\"10\",\"pageStartRow\":\"0\"},\"sys\":{\"appcode\":\"moa\",\"compressdata\":false,\"encryptdata\":\"true\",\"keycode\":\"5yvfrapr8552006067505393265302\",\"method\":\"report\"}}";

    NSMutableDictionary *dict1=[NSMutableDictionary dictionary];

    

    NSString *bodyStr = [NSString stringWithFormat:@"xmas-json=%@",bodyStr1];

    NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

    urlrequest.HTTPBody = body;

    AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest];

    requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

    [requestOperation start];

    [requestOperation waitUntilFinished];

    NSLog(@"RESPONSE%@",requestOperation.responseString);

    NSRange range = [requestOperation.responseString rangeOfString:@"\"msg\":\"0\""];

    if (range.location != NSNotFound) {

        isTrue = true;

    }

    if (!isTrue) {

        NSLog(@"错误", @"您需要联系开发人员");

    }


    

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值