IOS 如何使用 RESTFUL API 从 Web 服务中检索数据

标签 ios objective-c rest nsurlconnection nsxmlparser

我有这个 RESTFUL API 调用。我尝试连接到 Web 服务。响应成功接收但是当我注销数据接收时,它没有。请帮我。我的休息信息错了吗?

这是我的代码:

- (void)viewDidLoad {
    [super viewDidLoad];


    // Do any additional setup after loading the view.

    feeds = [[NSMutableArray alloc] init];

    NSString *restMessage = [NSString stringWithFormat:@"https://deepsightapi.symantec.com/v1/domains/www.google.com/"];



    NSURL *url = [NSURL URLWithString:restMessage];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];







    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    //[connection start];

    if(connection)
    {
        _webResponseData = [NSMutableData data] ;


    }
    else
    {
        NSLog(@"Connection is NULL");
    }

}
- (void)connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Response recieved");
    [self.webResponseData  setLength:0];
}

- (void)connection:(NSURLConnection*) connection didReceiveData:(NSData *)data
{
    NSLog(@"Data recieved");
    [self.webResponseData  appendData:data];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSLog(@"Received %lu Bytes", (unsigned long)[_webResponseData length]);
    NSString *theXML = [[NSString alloc] initWithBytes:
                        [_webResponseData mutableBytes] length:[_webResponseData length] encoding:NSUTF8StringEncoding];

    NSLog(@"hello %@",theXML);

    //now parsing the xml

    NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding];

    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData];

    //setting delegate of XML parser to self
    xmlParser.delegate = self;

    // Run the parser
    @try{
        BOOL parsingResult = [xmlParser parse];
        NSLog(@"parsing result = %hhd",parsingResult);
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:[exception reason] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
}

最佳答案

我用过这种方式,可以将你的响应转化为Dictionary或者Array。 请用下面给出的代码替换您的代码。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        NSLog(@"Succeeded! Received %d bytes of data",[_webResponseData length]);

        NSString *responseText = [[NSString alloc] initWithData:_webResponseData encoding: NSASCIIStringEncoding];
        NSLog(@"Response by server is : %@", responseText);
        NSError *e;

      //    Convert into Array
        NSMutableArray *object = [NSJSONSerialization JSONObjectWithData:_webResponseData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@",object[0]);
    //  Print your field 

     //  Convert into Dictionary

 NSMutableDictionary *objectDic = [NSJSONSerialization JSONObjectWithData:_webResponseData options:NSJSONReadingMutableContainers error:&e];
NSLog(@"%@",objectDic[@"your Key name"]);

如果有效,请告诉我。 谢谢

关于IOS 如何使用 RESTFUL API 从 Web 服务中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351199/

相关文章:

ios - 使用谷歌地图快速获取我的位置

json - 具有未知 key 的 JSON 序列化

asp.net - 在哪里可以查看 Azure 上 API 调用的错误 Azure "An error has occurred."的日志或详细信息?

json - 结构列表作为 json golang

ios - graph.facebook.com 的 DNS 问题

objective-c - 如何使用当前的模态视图 Controller 在 View Controller 之外呈现模态视图 Controller :animated?

ios - Protocol 只能用作泛型约束,因为它有 Self 或关联类型要求

objective-c - objective-c 到快速转换。 (#define 和弱)

ios - 在 TableView objective-c 中的按钮上添加操作

ios - 如何使用循环在行中仅显示 3 个按钮,在列中仅显示一个按钮