【问题标题】:Connecting to a RESTful webservice in objective-c在 Objective-C 中连接到 RESTful Web 服务
【发布时间】:2013-01-16 06:08:17
【问题描述】:

我尝试连接到 Web 服务并从中检索数据,但没有成功。这是我所做的。

这是我尝试连接到http://www.rcsb.org/pdb/software/rest.do的网络服务

这是我的 example.h 文件:

#import <Foundation/Foundation.h>

@interface example : NSObject {

    NSMutableData *receivedData;
}
@property(nonatomic, retain) NSMutableData *receivedData;

- (void) getDataFromServer;
@end

这是我的 example.m 文件:

导入“example.h”

@implementation example

@synthesize receivedData;

-(void) getDataFromServer {
    //prepare request
    NSString *urlString = [NSString stringWithFormat:@"http://www.rcsb.org/pdb/rest/search/"];
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
    [theRequest setURL:[NSURL URLWithString:urlString]];
    [theRequest setHTTPMethod:@"POST"];
    
    NSString *myXmlQuery = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><orgPdbQuery><version>head</version><queryType>org.pdb.query.simple.AdvancedKeywordQuery</queryType><description>Text Search for: chloro</description><keywords>chloro</keywords></orgPdbQuery>"];
    
    
    //set Headers
    NSString *contentType = [NSString stringWithFormat:@"application/xml"];
    [theRequest addValue:contentType forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:[NSString stringWithFormat:@"%ld", [myXmlQuery length]] forHTTPHeaderField:@"Content-Length"];
    
    //create the Body
    NSMutableData *postBody = [NSMutableData data];
    
    [postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[myXmlQuery dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
    
    
    //post
    [theRequest setHTTPBody:postBody];
    
    NSLog(@"%@", myXmlQuery);
    
    //get response
    NSHTTPURLResponse *urlResponse = [[NSHTTPURLResponse alloc] init];
    NSError *error = [[NSError alloc] init];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&urlResponse error:&error];
    NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response code- %ld",[urlResponse statusCode]);
    NSLog(@"Response: %@", result);
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"in didReceiveResponse....setting receivedData to zero");
    //[receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"In didReceiveData...receiving data and appending to receivedData");
    [receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Connection failed with error");
}

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

@end

我收到一个 html 页面作为对此的回应。此外,connectionDidFinishLoading 任何时候都不会被调用,因为我的输出中没有得到 NSLog 语句。我得到的只是我尝试连接的 url 的 html 版本。

我们将不胜感激任何形式的帮助。谢谢。

PS:我正在尝试正确格式化此文本,但它会自动像这样格式化。很抱歉给您带来不便。

【问题讨论】:

  • 这是次要的,但 REST URL 是 /pdb/rest/search。您附加了一个斜杠。一些网络服务对这些事情有点挑剔。

标签: objective-c web-services http-post


【解决方案1】:

看起来您正在尝试做两件不同的事情。首先,我建议您查看URL Loading System Programming Guide。您也不需要创建NSHTTPURLResponse*,因为它会为您处理好。

其次,您尝试使用NSURLConnectionDelegate 协议而不符合它,或者将您的类添加为委托。这与sendSynchronousRequest 有很大不同,后者将阻塞主线程,并且不需要委托集,因为该方法将等待响应并且不允许任何其他代码执行(因此阻塞线程)。有一个非常好的StackOverflow question and answer 与你的非常相似。

简而言之:

  1. 在您的界面中符合&lt;NSURLConnectionDelegate&gt;
  2. NSURLConnection* 创建一个属性
  3. 使用self.myConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  4. 现在您可以依赖被调用的NSURLConnectionDelegate 协议方法(connectionDidFinishLoading:connection:didReceiveData: 等)。
  5. 点击connectionDidFinishLoading: 后,即可开始使用下载的数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多