0

我不知道为什么,但我所做的一切都不会帮助解决这个问题。我只是无法从服务器获取 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}
4

1 回答 1

3

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

于 2012-11-14T08:46:42.507 回答