2016-12-15 24 views
0

我没有得到服务器端的响应。以下是我的代码。什么样的错误,请提前告诉我的感谢。如何获得我的服务器的响应,以及如何访问该数据

检查我的代码:

//NSString *jsonRequest = [NSString stringWithFormat:@"{\"Email\":\"%@\",\"FirstName\":\"%@\"}",user,fname]; 
NSLog(@"Request: %@", jsonRequest); 

NSURL *url = [NSURL URLWithString:@"http://golf.42point5.com/API/Jsonapi.php?method=singup"]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]]; 

[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody: requestData]; 

//NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]]; 
    NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:request 
                 completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
                  NSLog(@"Response:%@ %@\n", response, error); 
                  if(error == nil) 
                  { 

                   NSString * text = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding]; 
                       NSLog(@"Data = %@",text); 
                      } 

                 }]; 
    [dataTask resume]; 
+1

你做了什么样的错误? – belous

+0

我没有得到如下响应: –

+0

@Manishpatel没有得到类似的响应,是不是NSError对象的错误值是什么? – Larme

回答

0

您不使用HTTPS,请添加到您的的info.plist

<key>NSExceptionDomains</key> 
<dict> 
    <key>golf.42point5.com</key> 
    <dict> 
     <key>NSIncludesSubdomains</key> 
     <true/> 
     <key>NSExceptionAllowsInsecureHTTPLoads</key> 
     <true/> 
     <key>NSExceptionRequiresForwardSecrecy</key> 
     <true/> 
     <key>NSExceptionMinimumTLSVersion</key> 
     <string>TLSv1.2</string> 
     <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key> 
     <false/> 
     <key>NSThirdPartyExceptionRequiresForwardSecrecy</key> 
     <true/> 
     <key>NSThirdPartyExceptionMinimumTLSVersion</key> 
     <string>TLSv1.2</string> 
     <key>NSRequiresCertificateTransparency</key> 
     <false/> 
    </dict> 
</dict> 
+0

我已经完成了这一点。 –

+0

我编辑我的答案,请尝试 –

+0

谢谢Marat它正在为我工​​作。 –

相关问题