调用OADataFetcher fetchDataWithRequest POST时出现NSRangeException

时间:2013-05-27 04:28:31

标签: ios oauth

开始与实现Oauth的Web服务API进行通信。实现了OauthConsumer并开始使用GET方法构建和检索数据。一切正常,直到我尝试使用XML主体的POST方法 起初我开始咆哮错误的树木,直到我发现了一些帮助我更接近解决问题的金块

  • 我需要设置Content-Type
  • 我需要在设置setHTTPBody方法之前准备请求

    OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
                       consumer:consumer
                          token:accessToken
                          realm:nil
              signatureProvider:nil];
    
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
    [request prepare];
    
    [request setHTTPBody:[xml dataUsingEncoding:NSUTF8StringEncoding]];
    
    OADataFetcher *fetcher = [[OADataFetcher alloc] init];
    
    [fetcher fetchDataWithRequest:request
                     delegate:self
            didFinishSelector:@selector(apiTicket:didPostInvoice:)
              didFailSelector:@selector(apiTicket:didFailPostInvoice:)];
    

然后我在调用fetchDataWithRequest时开始获取NSRangeException。

如何阻止这种情况发生?

1 个答案:

答案 0 :(得分:0)

事实证明问题是fetchDataWithRequest调用了prepare方法,这解除了导致获取数据的所有好工作。解决方案是创建一个fetchDataWithPreparedRequest方法,以确保不再调用prepare

@implementation OADataFetcher
...

- (void)fetchDataWithPreparedRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector {
        [request release];
        request = [aRequest retain];
        delegate = aDelegate;
        didFinishSelector = finishSelector;
        didFailSelector = failSelector;

//      [request prepare];

        connection = [[NSURLConnection alloc] initWithRequest:aRequest delegate:self];
}

@end