2016-12-01 44 views
0

我正在使用Google云端点与数据存储。当我通过Android & iOS方法通过POST方法传递数据时,它将被保存在具有空记录的数据存储中。即请求正常工作,但我发送数据的方式是错误的。请帮助我。 对于iOS,代码如下所述:通过iOS发送数据到谷歌云端点时,请求为空

NSDictionary *headers = @{ @"content-type": @"application/json", 
          @"cache-control": @"no-cache", 
          @"postman-token": @"404012ff-722e-c5d8-48db-fa7fb3260841"}; 
NSLog(@"header %@",headers); 
request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"enter url here..."] 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:10.0]; 
NSString* [email protected]""; 
strRequest =[[NSString alloc] initWithFormat:@"displayEmail=%@&displayName=%@",@"[email protected]",@"sonal"]; 
NSLog(@"url is %@ parameter = %@", request , strRequest); 
[request setHTTPMethod:@"POST"]; 
[request setHTTPBody: [strRequest dataUsingEncoding:NSUTF8StringEncoding]]; 
[request setAllHTTPHeaderFields:headers]; 
NSURLSession *session = [NSURLSession sharedSession]; 

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request 
              completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
               NSString *responseBody = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
               NSLog(@"responce Body===>%@",responseBody); 
               if (error) { 
                NSLog(@"%@", error); 
               } else { 
                NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
                NSLog(@"responce%@", httpResponse); 
               } 
              }]; 
[dataTask resume]; 
+0

如何你发现请求是空白的吗?我们需要更多的代码。 – Poles

+0

我编辑我的代码..请看看,让我知道 –

+0

代码看起来不错。但是如何说明请求是空白的。 – Poles

回答

0

我们得到了一个解决方案.. 我们改变了传递数据的方式.. 这里是代码:

request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:syncURL] 
     cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60]; 
     NSData *requestData = [NSData dataWithBytes:[serverSyncAPIRequestJSON UTF8String] length:[serverSyncAPIRequestJSON length]];[request setHTTPMethod:@"POST"]; [request setHTTPBody:requestData]; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];