使用AFNetworking发送嵌套JSON

使用AFNetworking发送嵌套JSON

本文介绍了使用AFNetworking发送嵌套JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要将注册数据发送到服务器,我使用的JSON格式如下:

To send registration data to server, I am using JSON is in following form:

{"regData": {
 "City":"Some City",
 "Country":"Some Country",
 "Email_Id":"[email protected]",
 "MobileNumber":"+00xxxxxxxxxx",
 "UserName":"Name Of user"
 }
}

以下是发送方式。

 NSURL * url = [[NSURL alloc] initWithString:registerUrlString];
            AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
            httpClient.parameterEncoding = AFJSONParameterEncoding;
            [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
            NSDictionary * params = @{@"regData": @{
                                              @"City": self.cityField.text,
                                              @"Country": self.countryField.text,
                                              @"Email_Id": self.emailField.text,
                                              @"MobileNumber": self.numberField.text,
                                              @"UserName": self.userName.text,
                                              }
                                      };

            NSMutableURLRequest * request = [httpClient requestWithMethod:@"POST" path:registerUrlString parameters:params];
            AFHTTPRequestOperation * operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                NSLog(@"Success: %@", JSON);

            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
            NSLog(@"Error: %@", [error debugDescription]);
            }];

            [operation start];

但遗憾的是我收到此错误:

But unfortunately I am getting this error:

错误Domain = NSCocoaErrorDomain Code = 3840操作无法完成。(Cocoa error 3840。)(JSON文本不是以数组或对象开头,而是选项允许未设置片段。) UserInfo = 0x94b3c30 {NSDebugDescription = JSON文本没有以数组或对象开头,并且选项允许未设置片段。}

推荐答案

您的要求没问题。由于您的服务器使用无效的JSON对象进行响应,因此返回错误错误域= NSCocoaErrorDomain Code = 3840 NSLog operation.responseString 查看发回的内容。

Your request is fine. The error Error Domain=NSCocoaErrorDomain Code=3840 is being returned because your server is responding with invalid JSON object. NSLog operation.responseString to see what's being sent back.

这篇关于使用AFNetworking发送嵌套JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 05:47