SoFunction
Updated on 2025-04-03

IOS uses AFN to send Json data in string form to server instance

Everyone has different needs in the background. I can only write down the needs and practices I have encountered at the moment. My background needs a Json key, corresponding to a dictionary of string type. The background requires a dictionary with double quotes and colons, so I spliced ​​it.

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  // Send post request//   = [AFJSONResponseSerializer serializer];
   = [AFJSONResponseSerializer serializer];
   = [NSSet setWithObject:@"text/html"];
  //
  =[AFHTTPRequestSerializer serializer];
  NSDictionary *json = [NSDictionary dictionary];
   NSString *str = [NSString stringWithFormat:@"{\"city\":\"%@\",\"country\":\"%@\",\"headimgurl\":\"%@\",\"language\":\"%@\",\"openid\":\"%@\",\"province\":\"%@\",\"sex\":%@,\"unionid\":\"%@\",\"userName\":\"%@\"}",[@"city"],[@"country"],[@"headimgurl"],[@"language"],[@"openid"],[@"province"],[@"sex"],[@"unionid"],[@"nickname"]];
  json = @{
//      @"city":[@"city"],
//      @"country":[@"country"],
//      @"headimgurl":[@"headimgurl"],
//      @"language":[@"language"],
//      @"openid":[@"openid"],
//      @"province":[@"province"],
//      @"sex":[@"sex"],
//      @"unionid":[@"unionid"],
//      @"userName":[@"nickname"],
      @"json":str,
//      @"json":@"json"
      
          };
  NSLog(@"post debug %@",json);
  NSString *URLStr = @"http://10.62.16.247:8080/";
  
  [manager POST:URLStr parameters:json progress:^(NSProgress * _Nonnull uploadProgress) {
    
  } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
    NSString *str = responseObject;
    NSLog(@"postCallback====%@",str);
    NSLog(@"Post Success");
  } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    NSLog(@"Post failed");
  }];

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.