2012-11-14 77 views
0

我不知道爲什麼,但是我所做的一切都不會幫助解決這個問題。我無法從服務器獲取JSON數據。ASIHTTPRequest錯誤不斷出現

這是編碼的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的其他網址,它完美的作品,但在這一個我保持gettings此錯誤:

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

回答

3

你的URL編碼您的網址,並且不再有一個有效的方案(HTTP://gone,剩下的就是http%25),它不會在Web瀏覽器中加載,更不用說http庫了。您可能打算對查詢參數進行URL編碼。有關更多信息,請參閱http://en.wikipedia.org/wiki/Percent-encoding