1

我正在尝试使用 OAConsumer API 获取数据。我尝试了以下代码,这给了我一个错误。

-(void)performQuery
{
 OAConsumer *consumer = [[OAConsumer alloc] initWithKey:OAUTH_KEY
                                                  secret:OAUTH_SECRET];

 NSURL *url = [NSURL URLWithString:@"https://api.semantics3.com/v1/products"];

 OAMutableURLRequest *request = [[OAMutableURLRequest alloc] initWithURL:url
                                                               consumer:consumer
                                                                  token:nil   
                                                                  realm:nil  
                                                      signatureProvider:nil]; // use the default method, HMAC-SHA1

  [request setHTTPMethod:@"GET"];
  [consumer release];


  OARequestParameter * qParam = [[OARequestParameter alloc] initWithName:@"q" value:@"{\"cat_id\":13658,\"model\":\"Satellite\"}"];


 NSArray *params = [NSArray arrayWithObjects:qParam, nil];
 [qParam release];

 [request setParameters:params];
 OADataFetcher *fetcher = [[OADataFetcher alloc] init];

 [fetcher fetchDataWithRequest:request
                     delegate:self
            didFinishSelector:@selector(requestDidFinishWithData:)
              didFailSelector:@selector(requestDidFailWithError:)];

  [request release];

}

错误如下:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OAServiceTicket bytes]: unrecognized selector sent to instance 0x1ed65bf0'

错误发生在这一行: **NSString response= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

  - (void)requestDidFinishWithData:(NSData *)data {

    NSString *response= [[NSString alloc] initWithData:data
                                                   encoding:NSUTF8StringEncoding];

    //here do what you want with the response, I use SBJson to parse it.

    NSLog(@"Request success!%@", response); 
    [responseBody release];


 }


  - (void)requestDidFailWithError:(NSError *)error {

    NSLog(@"Request did fail with error: %@", [error localizedDescription]);

  }

知道我在这里做什么吗?

4

2 回答 2

1

当它实际上是一个 OAServiceTicket 对象时,您正试图读取为 NSData 。

OAServiceTicket 对象具有以下内容:

request = [aRequest retain];
response = [aResponse retain];
data = [aData retain];
didSucceed = success;

所以你的代表必须如下:

- (void)requestDidFinishWithData:(OAServiceTicket *)ticket {

    NSString *response= [[NSString alloc] initWithData:ticket.data
                                              encoding:NSUTF8StringEncoding];

    //here do what you want with the response, I use SBJson to parse it.

    NSLog(@"Request success!%@", response);
    [response release];

}

为我工作。

于 2013-10-16T03:00:52.243 回答
0

请问ticket是否成功,像这样...

 - (void)fetchingVideoTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data{
if(ticket.didSucceed)
{
  your code...
 }

在您的情况下,请求似乎成功,但没有生成响应,表明您没有包含该请求的所有必需参数,当我尝试连接 Vimeo 但添加回调参数后,我也遇到了同样的问题到请求,连接成功并生成响应。检查是否已添加所有参数。

于 2013-09-11T09:45:51.177 回答