OAConsumer API thows an error on receive data
I'm trying to fetch data using OAConsumer API. I tried the following code which threw me an error.
-(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];
}
The error is as follows:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[OAServiceTicket bytes]: unrecognized selector sent to instance 0x1ed65bf0'
The error happens on this line: **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]);
}
Any idea what I'm doing here?
You are trying to read as NSData when it is actually an OAServiceTicket object.
An OAServiceTicket object has the following:
request = [aRequest retain];
response = [aResponse retain];
data = [aData retain];
didSucceed = success;
So your delegate has to be as follows:
- (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];
}
Works for me.
please whether the ticket is success, like this...
- (void)fetchingVideoTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data{
if(ticket.didSucceed)
{
your code...
}
it seem in your case the request is success but there is no response generated which states that you have not included all the required parameters for that request, i also got the same problem when i tried to connect with Vimeo but after i added the callback parameter to the request, the connection is successful and response is generated. Check whether you have added all the parameters.
链接地址: http://www.djcxy.com/p/68088.html