1

您好我正在开发小型 IOS 应用程序,我想在其中发送 json 数组作为发布数据。我以以下方式尝试过:

NSString *sampleParam = @"[{\"user:\"\"me Nilesh \"}]";
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

NSString *urlString = @"ABC.com";

NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:60.0];

[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[sampleParam dataUsingEncoding:NSUTF8StringEncoding]];

NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                                   completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
                                  {
                                      dispatch_async(dispatch_get_main_queue(),^
                                     {
                                       // handle result here ... 
                                     });
                                 }];
                                [dataTask resume];

我有这样的日志记录

 -[GCKDeviceScanner startNetServiceScan]  startNetServiceScan
 -[GCKDeviceScanner stopNetServiceScan]  stopNetServiceScan

状态码为零,错误是请求超时。

所以这是我的场景,我试图将 json 数组数据发送到我的服务器。一旦我在一段时间后开始我的 API 调用,它就会给我状态码为零并请求超时错误。但主要的是它正在更新我的服务器上的数据。那么为什么它给出状态码为零的错误。有什么我做错了或我错过了什么。它不断给出该错误。是不是因为 NSURLSessionConfiguration.

4

1 回答 1

0

也许您的服务器需要在您的 POST 请求中指定内容类型:

[urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
于 2015-01-17T16:32:40.393 回答