2013-01-16 77 views
0

我尝试连接到web服务并从中检索数据,但我没有成功。这是我所做的。连接到objective-c中的REST风格的web服务

这是我试图连接到http://www.rcsb.org/pdb/software/rest.do

这里是我的example.h文件文件中的Web服务:

#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:我试图正确格式化这个文本,但它会自动得到这样的格式。抱歉给你带来不便。

+1

它很小,但REST URL是'/ pdb/rest/search'。你有附加的斜线。一些网络服务对这些事情有点挑剔。 –

回答

0

看起来你正在尝试做两件不同的事情。首先,我建议你看看URL Loading System Programming Guide。您也不需要创建一个NSHTTPURLResponse*,因为它会得到您的照顾。其次,您试图使用NSURLConnectionDelegate协议而不遵从它,或者将您的类作为委托添加。这与做sendSynchronousRequest有很大不同,它会阻塞主线程,并且不需要委托集,因为该方法将等待响应并且不允许任何其他代码执行(因此阻塞该线程)。有一个非常好的StackOverflow question and answer非常相似你的。

一言以蔽之:

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