我一直得到:无法在请求上创建请求(错误的网址?)错误消息,如:
- (void)grabURLInBackground :(id)sender
{
NSURL *url= [NSURL URLWithString:@"http://api.website.com/api_requests/standard_request/getItemRequest={\"Id\":\"Logic\"}"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request addRequestHeader:@"X-WEBSITE-API-DEV-NAME" value:@"cghjm"];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestFinished:(ASIHTTPRequest *)request
{
// Use when fetching text data
NSString *responseString = [request responseString];
NSLog(responseString);
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"%@:%s Error: %@", [self class], _cmd, [error localizedDescription]);
}
我对Objective-C非常“新”......
由于
-Brad
答案 0 :(得分:4)
将-startAsynchronous
更改为-startSynchronous
,然后删除您将请求的delegate
设置为self
的行。异步意味着它在后台运行,在这种情况下,您需要使用委托方法。由于您已将所有内容放在一个方法中,因此您希望使用同步请求。
像这样初始化URL:
[NSURL URLwithString: [@"http://..." stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]]