简体   繁体   English

为什么用于 Web 服务的 RESTful 不适用于 iOS(提供的代码)?

[英]Why doesn't RESTful for web services work for iOS (Code provided)?

I used the following code to test out fetching data from the provided link in the code, but both data and response are nil.我使用以下代码来测试从代码中提供的链接获取数据,但数据和响应都为零。

RestViewController.h 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 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

In the storyboard, there are 3 UI components, two text labels and one button.在故事板中,有 3 个 UI 组件、两个文本标签和一个按钮。 Once the button (fetchGreeting) is clicked, the labels should fetch the data from the link and update the two text labels (greetingID and greetingContent) with the right data.单击按钮 (fetchGreeting) 后,标签应从链接中获取数据并使用正确的数据更新两个文本标签(greetingID 和 greetingContent)。

I followed the instructions from the following link exactly, but it still doesn't seem to fetch any data and can't seem to figure it out.我完全按照以下链接中的说明进行操作,但它似乎仍然没有获取任何数据并且似乎无法弄清楚。 Any guidance would be greatly appreciated.任何指导将不胜感激。

Your code has a type here:您的代码在这里有一个类型:

- (IBAction)fetchGreeting;

Should be:应该:

- (IBAction)fetchGreeting

Also, you're using a deprecated method for IOS9, which is this:此外,您正在使用 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"];
     }
 }];

Also, if you're planning to work with IOS9, you need to either use HTTPS or temporarily allow HTTP connections by adding this to your project's PLIST.此外,如果您计划使用 IOS9,则需要使用 HTTPS 或通过将其添加到项目的 PLIST 来暂时允许 HTTP 连接。

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

Here's an updated method:这是一个更新的方法:

    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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM