在objective-c中连接到RESTful Web服务

时间:2013-01-16 06:08:17

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

我尝试连接到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文件:

import“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语句。我得到的只是我正在尝试连接的网址的html版本。

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

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

1 个答案:

答案 0 :(得分:0)

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

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

简而言之:

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