开发者

How to get the response string and parse it using ASIHTTPRequest?

I am using ASIHTTPRequest to connect to a web service. I am not getting what I should do next. When i run the code provided below I am not getting any output or response string. Program is running without any errors. What should I do to get the response string and parse it?

Code

-(void)callWebService
{

    //this is a typical url for REST webservice, where you can specify the method that you want to call and the parameters directly with GET

    NSURL *url = [NSURL URLWithString:@"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures"];
    //https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures&callback=handleResponse    

    ASIHTTPRe开发者_高级运维quest *request = [ASIHTTPRequest requestWithURL:url];

    [request setDidFinishSelector:@selector(requestCompleted:)];
    [request setDidFailSelector:@selector(requestError:)];

    [request setDelegate:self];
      // [request setRequestMethod:@"GET"];
    [request startAsynchronous];
}

- (void)requestCompleted:(ASIHTTPRequest *)request
{
   NSString *responseString = [request responseString];
}

- (void)requestError:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}


Your code is working just fine. Just add some NSLogs in your delegate methods. NSLog(@"ResponseString:%@",responseString); and log the error to NSLog(@"Error:%@",[error description]);.

May be your problem is from the default time out of 10 sec for ASIHttpRequest.

Edit:

- (void)requestCompleted:(ASIHTTPRequest *)request
{
    NSString *responseString = [request responseString];
    NSLog(@"ResponseString:%@",responseString);
}

- (void)requestError:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"Error:%@",[error description]);
}

Edit 2:

 if([responseString length]){
        // do some thing
    }

To convert from NSString to NSDictionary just use json-framework.

NSDictionary *returnDict = [responseString JSONValue];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜