我不断收到:Unable to create request (bad url?) 错误消息,如:
- (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 的“非常”新手...
谢谢
-布拉德
最佳答案
将 -startAsynchronous
更改为 -startSynchronous
并删除将请求的 delegate
设置为 self
的行。异步意味着它在后台运行,在这种情况下您需要使用委托(delegate)方法。由于您已将所有内容放在一个方法中,因此您希望使用同步请求。
编辑
像这样初始化 URL:
[NSURL URLwithString: [@"http://..." stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]]
关于iphone - ASIHTTPRequest 无法创建请求(错误的 URL?)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3743046/