IOS用AFN发送字符串形式的Json数据给服务器实例
程序员文章站
2023-12-20 08:11:22
每个人遇到后台的需求不一样,我只能写出自己目前遇到的需求和做法,我的后台是需要一个json的key,对应一个字符串类型的字典,后台要求的是带双引号和冒号的字典,所以就拼接了...
每个人遇到后台的需求不一样,我只能写出自己目前遇到的需求和做法,我的后台是需要一个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失败"); }];
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。