问题描述
|
我正在使用
ASIHTTPRequest
连接到Web服务。我没有下一步该做什么。当我运行下面提供的代码时,我没有得到任何输出或响应字符串。程序正在运行,没有任何错误。我应该怎么做才能获取响应字符串并解析它?
码
-(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
ASIHTTPRequest *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];
}
解决方法
您的代码工作正常。只需在委托方法中添加一些NSLogs。
NSLog(@\"ResponseString:%@\",responseString);
,并将错误记录到NSLog(@\"Error:%@\",[error description]);
。
可能是您的问题是ASIHttpRequest的默认时间超出10秒。
编辑:
- (void)requestCompleted:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSLog(@\"ResponseString:%@\",responseString);
}
- (void)requestError:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@\"Error:%@\",[error description]);
}
编辑2:
if([responseString length]){
// do some thing
}
要从NSString转换为NSDictionary,只需使用json-framework。
NSDictionary *returnDict = [responseString JSONValue];