2016-03-07 14 views
-1

我用下面的代碼來測試從代碼中提供的鏈接中獲取數據,但是數據和響應都是零。爲什麼RESTful for web服務不適用於iOS(代碼提供)?

RestViewController.h

#import <UIKit/UIKit.h> 

@interface RestViewController : UIViewController 

@property (nonatomic, strong) IBOutlet UILabel *greetingId; 
@property (nonatomic, strong) IBOutlet UILabel *greetingContent; 

- (IBAction)fetchGreeting; 

@end 

RestViewController.m

#import "RestViewController.h" 

@interface RestViewController() 

@end 

@implementation RestViewController 

- (IBAction)fetchGreeting; 
{ 
    NSURL *url = [NSURL URLWithString:@"http://rest-service.guides.spring.io/greeting"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [NSURLConnection sendAsynchronousRequest:request 
             queue:[NSOperationQueue mainQueue] 
          completionHandler:^(NSURLResponse *response, 
               NSData *data, NSError *connectionError) 
    { 
     if (data.length > 0 && connectionError == nil) 
     { 
      NSDictionary *greeting = [NSJSONSerialization JSONObjectWithData:data 
                     options:0 
                     error:NULL]; 
      self.greetingId.text = [[greeting objectForKey:@"id"] stringValue]; 
      self.greetingContent.text = [greeting objectForKey:@"content"]; 
     } 
    }]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self fetchGreeting]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

@end 

在情節串連圖板,有3個UI組件,兩個文本標籤和一個按鈕。點擊按鈕(fetchGreeting)後,標籤應從鏈接中提取數據,並使用正確的數據更新兩個文本標籤(greetingID和greetingContent)。

我完全按照以下鏈接中的說明進行操作,但它似乎仍無法獲取任何數據,但似乎無法弄清楚。任何指導將不勝感激。

+0

這是ios9如果是沒有ü處理與HTTP安全異常? – bolnad

+0

@bolnad我使用xcode的mac是OS X –

+0

沒有兄弟我的意思是你的發展目標 – bolnad

回答

0

代碼有型的位置:

- (IBAction)fetchGreeting; 

應該是:

- (IBAction)fetchGreeting 

此外,您使用的是IOS9棄用的方法,該方法是這樣的:

[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse *response, 
              NSData *data, NSError *connectionError) 
{ 
    if (data.length > 0 && connectionError == nil) 
    { 
     NSDictionary *greeting = [NSJSONSerialization JSONObjectWithData:data 
                    options:0 
                    error:NULL]; 
     self.greetingId.text = [[greeting objectForKey:@"id"] stringValue]; 
     self.greetingContent.text = [greeting objectForKey:@"content"]; 
    } 
}]; 

另外,如果您打算使用IOS9,則需要使用HTTPS或通過將此連接添加到y來暫時允許HTTP連接我們的項目的PLIST。

<key>NSAppTransportSecurity</key> 
<dict> 
    <key>NSAllowsArbitraryLoads</key><true/> 
</dict> 

這裏有一個更新的方法:

NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    if (data.length > 0 && error == nil) 
    { 
     NSDictionary *greeting = [NSJSONSerialization JSONObjectWithData:data 
                   options:0 
                    error:NULL]; 
     self.greetingId.text = [[greeting objectForKey:@"id"] stringValue]; 
     self.greetingContent.text = [greeting objectForKey:@"content"]; 
    } 
}]; 
[downloadTask resume]; 
相關問題