iphone - ASIHTTPRequest 无法创建请求(错误的 URL?)错误

标签 iphone objective-c ios asihttprequest

我不断收到: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/

相关文章:

ios - 应用程序激活后图像比较不起作用

html - 如何在 TTTAttributedLabel 中将 HTML anchor 设为可点击链接?

iphone - 将 NSArray 作为 otherButtonTitles 传递 - iOS

ios - 在 UITableView 中滚动时图像变得困惑

ios - 旋转设备时是否可以对 UIView 使用自定义旋转?

iphone - 正确实现委托(delegate)和协议(protocol)

ios - iOs Xcode 中的 UIViewController 中的 UIView 中的 UITableView

ios - 我如何在我自己的 iOS 项目中使用 pjsip?

ios - 向下和向上滚动后 TableView 单元格与 UIView 重叠

ios - 方法 Swizzling 未正确触发?