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