【问题标题】:ASIHTTPRequest Unable to create request (bad url?) ErrorASIHTTPRequest 无法创建请求(错误的 url?)错误
【发布时间】:2011-04-14 04:05:42
【问题描述】:

我不断收到:无法在请求中创建请求(错误的 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 的“非常”新手...

谢谢

-布拉德

【问题讨论】:

    标签: iphone objective-c ios asihttprequest


    【解决方案1】:

    -startAsynchronous 更改为-startSynchronous 并删除将请求的delegate 设置为self 的行。异步意味着它在后台运行,在这种情况下您需要使用委托方法。由于您已将所有内容放在一个方法中,因此您希望使用同步请求。

    编辑

    像这样初始化 URL:

    [NSURL URLwithString: [@"http://..." stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]]
    

    【讨论】:

    • 我尝试了两种方式...实际上我也有 requestFinished 和 requestFailed 函数...我正在更新代码以显示我所拥有的确切内容,因为我忘记包含该部分。
    • 我认为问题与 URL 中的 {} 有关。虽然我对其他语言没有问题,php,delphi...
    • 这是一个问题,网站需要大括号和 URL (JSON) 中的双引号,并且 NSUrl 不允许这些字符出现在 URL 中。感谢您的帮助,我能够验证问题并请求更新 API 以解决问题。 stringByAddingPercentEscapesUsingEncoding 是我需要添加的。
    • 对我有用的是:stackoverflow.com/questions/705448/… - Roger 手动替换了字符串中的有问题的字符。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-11
    • 1970-01-01
    • 2017-03-27
    • 2017-11-16
    • 1970-01-01
    • 2018-11-28
    • 2020-04-13
    相关资源
    最近更新 更多