与Server端进行Json数据的POST操作

由于最近项目在网络通信方面需要传输Json格式,所以在网上搜索了一下关于Json的数据传输:关于AFNetWorking的很多例子不可用,经过搜集资料,最终调通了与Server的信息交互,具体交互方式如下,亲测可行:

1:ASI与Server的交互。

NSMutableDictionary *myArray =  @{@"imei":@"napq3MyaOOUjaU6necZ5lDLslmQ=",@"imsi":@"AlUAwfYVf3ZGiFZZDVruer7cW4Q=",@"wifiSSID":@"112233",@"osVersion":@"Android4.1.2",@"mobileID":@"1112733557413931973",@"channelID":@"4139823094061327276",@"deviceName":@"HUAWEIG730-C00",@"date":@"12345678",@"id":@"112233"};

    NSMutableDictionary *jsonDic = [NSMutableDictionarydictionary];

    [jsonDicsetObject:myArray forKey:@"deviceInfo"];

    [jsonDicsetObject:@"juexing"forKey:@"name"];

    [jsonDic setObject:@"15800000000"forKey:@"mobile"];

    [jsonDicsetObject:@"1.0"forKey:@"version"];

   NSString *strJson = [jsonDic JSONString];

   NSLog(@"%@",strJson);

   NSError *error;

     if ([NSJSONSerializationisValidJSONObject:jsonDic])

     {

         NSData *jsonData = [NSJSONSerializationdataWithJSONObject:jsonDic options:NSJSONWritingPrettyPrintederror: &error];

        NSMutableData *tempJsonData = [NSMutableDatadataWithData:jsonData];

         ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];

         [request addRequestHeader:@"Content-Type"value:@"application/json; encoding=utf-8"];

         [request addRequestHeader:@"Accept"value:@"application/json"];

         [requestsetRequestMethod:@"POST"];

         [requestsetPostBody:tempJsonData];

         request.delegate =self;

         [requestsetTimeOutSeconds:10];  //超时

         [requeststartSynchronous];

        NSError *error1 = [request error];

        if (!error1) {

             applyresponse = [requestresponseString];

            NSLog(@"Test%@",applyresponse);

             

         }

     }

2:AFNetWorking与Server交互。

 NSMutableDictionary *dic = [NSMutableDictionarydictionary];

    [dicsetObject:myArray forKey:@"deviceInfo"];

    [dic setObject:@"juexing"forKey:@"name"];

    [dic setObject:@"15800000000"forKey:@"mobile"];

    [dic setObject:@"1.0"forKey:@"version"];

    

    AFHTTPRequestOperationManager  *manager = [AFHTTPRequestOperationManagermanager];

    

    manager.requestSerializer = [AFJSONRequestSerializerserializer];

    manager.responseSerializer = [AFImageResponseSerializerserializer];

    

    [manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"application/json; encoding=utf-8"];

    [manager.requestSerializersetValue:@"application/json"forHTTPHeaderField:@"Accept" ];

     manager.responseSerializer.acceptableContentTypes = [NSSetsetWithObject:@"application/json"];

    

    

    [manager POST:[NSStringstringWithFormat:@"%@",url]parameters:dic success :^(AFHTTPRequestOperation *operation, id responseObject) {

        

       NSLog(@"json--:%@",operation.responseString);

       NSLog@"JSON====: %@" , responseObject);

        NSDictionary *dicPara = [NSJSONSerializationJSONObjectWithData:operation.responseDataoptions:NSJSONReadingAllowFragmentserror:nil];

       NSLog(@"dic---%@",dicPara);

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

        

       NSLog@"Error====: %@" , error. description );

       NSLog@" 返回值 :%@" ,operation.responseString);

        

    }];






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值