2016-12-15 22 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它正在爲我工​​作。 –

相關問題