objective-c - ASIHTTPRequest 错误不断出现

标签 objective-c ios json request asihttprequest

我不知道为什么,但我所做的一切都无助于解决这个问题。我只是无法从服务器获取 JSON 数据。

这是我得到的编码 NSString:

http%253A%252F%252Fwww.doctors.co.il%252Fmobile%252Fforum%252Fforum-4863%253Fpage%253D12%252F

这是我的代码:

- (NSString *)urlencode {
    NSMutableString *output = [NSMutableString string];
    const unsigned char *source = (const unsigned char *)[self UTF8String];
    int sourceLen = strlen((const char *)source);
    for (int i = 0; i < sourceLen; ++i) {
        const unsigned char thisChar = source[i];
        if (thisChar == ' '){
            [output appendString:@"+"];
        } else if (thisChar == '.' || thisChar == '-' || thisChar == '_' || thisChar == '~' ||
                   (thisChar >= 'a' && thisChar <= 'z') ||
                   (thisChar >= 'A' && thisChar <= 'Z') ||
                   (thisChar >= '0' && thisChar <= '9')) {
            [output appendFormat:@"%c", thisChar];
        } else {
            [output appendFormat:@"%%%02X", thisChar];
        }
    }
    return output;
}

- (void)startLoadingApplicationDataOf
{
    NSString *raw = @"http://www.doctors.co.il/mobile/forum/forum-4863?page=12/";
    NSString *safestring = [raw urlencode];
    NSURL *requestURL = [NSURL URLWithString:safestring];
    [self startRequest:requestURL];
}

- (void)startRequest:(NSURL *)requestURL
{
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:requestURL];
    [request setDidFinishSelector:@selector(requestCompleted:)];
    [request setDidFailSelector:@selector(requestError:)];
    [request setDelegate:self];
    [request startAsynchronous];
    [request setTimeOutSeconds:20.0f];
}

我做错了什么?在其他返回 JSON 的网址上,它工作得很好,但在这个网址上我一直收到这个错误:

Failed to load data with error: Error Domain=ASIHTTPRequestErrorDomain Code=6 "Unable to start HTTP connection" UserInfo=0x8bc72b0 {NSLocalizedDescription=Unable to start HTTP connection}

最佳答案

你已经对你的 url 进行了 url 编码,并且不再有一个有效的方案(http://消失了,剩下的是 http%25),它不会加载到Web 浏览器,更不用说 http 库了。您可能打算对查询参数进行 URL 编码。参见 http://en.wikipedia.org/wiki/Percent-encoding获取更多信息。

关于objective-c - ASIHTTPRequest 错误不断出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13374530/

相关文章:

ios - Weirdcore 数据 react - 未检测 NSManagedObject 子类

javascript - NG-repeat - 通过具有不同值的对象常量属性过滤掉

php - 以 json 形式返回表行并打印

objective-c - 更改 NSDatePicker 的默认突出显示颜色

ios - 在 iOS 中使用 GCD 实现临界区

ios - 类在两者中都实现,将使用两者之一。哪个是未定义的

javascript - JSON解析错误: Unexpected identifier

objective-c - 这些 if(0) 条件句有什么意义?

ios - 使用同一对象 NSNotificationCenter 释放调用两次

iphone - 可以从 Application Delegate 使用 UIActionSheet 吗?