【问题标题】:IOS sending jsonArray as POST dataIOS 发送 jsonArray 作为 POST 数据
【发布时间】:2015-03-16 00:00:46
【问题描述】:

您好,我正在开发小型 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 的缘故。

【问题讨论】:

    标签: ios json post nsurlsessiondatatask


    【解决方案1】:

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

    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-08
      • 1970-01-01
      • 1970-01-01
      • 2011-10-18
      • 2016-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多