ASIHTTPRequest错误不断出现

时间:2012-11-14 07:20:09

标签: 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}

1 个答案:

答案 0 :(得分:3)

你已经对你的网址进行了网址编码,并且不再有一个有效的方案(http://已消失,剩下的是http%25),这也不会在网络浏览器中加载,更不用说一个http库了。您可能打算对查询参数进行URL编码。有关详细信息,请参阅http://en.wikipedia.org/wiki/Percent-encoding