用AFN发送字符串形式的Json数据给服务器

每个人遇到后台的需求不一样,我只能写出自己目前遇到的需求和做法,我的后台是需要一个Json的key,对应一个字符串类型的字典,后台要求的是带双引号和冒号的字典,所以就拼接了一下


AFHTTPSessionManager *manager =  [AFHTTPSessionManager manager];

    // 发送post请求

//    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    manager.responseSerializer = [AFJSONResponseSerializer serializer];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

    //

    manager.requestSerializer=[AFHTTPRequestSerializer serializer];

    NSDictionary *json = [NSDictionary dictionary];

     NSString *str = [NSString stringWithFormat:@"{\"city\":\"%@\",\"country\":\"%@\",\"headimgurl\":\"%@\",\"language\":\"%@\",\"openid\":\"%@\",\"province\":\"%@\",\"sex\":%@,\"unionid\":\"%@\",\"userName\":\"%@\"}",self.userInfoDict[@"city"],self.userInfoDict[@"country"],self.userInfoDict[@"headimgurl"],self.userInfoDict[@"language"],self.userInfoDict[@"openid"],self.userInfoDict[@"province"],self.userInfoDict[@"sex"],self.userInfoDict[@"unionid"],self.userInfoDict[@"nickname"]];

    json = @{

//           @"city":self.userInfoDict[@"city"],

//           @"country":self.userInfoDict[@"country"],

//           @"headimgurl":self.userInfoDict[@"headimgurl"],

//           @"language":self.userInfoDict[@"language"],

//           @"openid":self.userInfoDict[@"openid"],

//           @"province":self.userInfoDict[@"province"],

//           @"sex":self.userInfoDict[@"sex"],

//           @"unionid":self.userInfoDict[@"unionid"],

//           @"userName":self.userInfoDict[@"nickname"],

            @"json":str,

//           @"json":@"json"

           

                   };

    NSLog(@"post调试%@",json);

    NSString *URLStr = @"http://10.62.16.247:8080/LoginServlet.bmh";

    

    [manager POST:URLStr parameters:json progress:^(NSProgress * _Nonnull uploadProgress) {

        

    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSString *str = responseObject;

        NSLog(@"post回调====%@",str);

        NSLog(@"post成功");

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

        NSLog(@"post失败");

    }];


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值