ios - AFNetWork3.0 Error Domain=NSURLErrorDomain Code=-1005。
PHP中文网
PHP中文网 2017-04-17 17:33:16
0
2
1036

网络请求失败:Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSUnderlyingError=0x7ff218465150 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=http://192.168.1.89:8888/shihai_snatch/v1/u/userBuyList, NSErrorFailingURLKey=http://192.168.1.89:8888/shihai_snatch/v1/u/userBuyList, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4, NSLocalizedDescription=The network connection was lost.}}, NSErrorFailingURLStringKey=http://192.168.1.89:8888/shihai_snatch/v1/u/userBuyList, NSErrorFailingURLKey=http://192.168.1.89:8888/shihai_snatch/v1/u/userBuyList, _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-4, NSLocalizedDescription=The network connection was lost.}

经常出现这种情况 请问怎么解决

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
巴扎黑

It looks like the network is down...or the server is shut down...

刘奇

There are two special characters ":" in your URL. The first ":" can be recognized normally, and the second ":" is the port number and cannot be recognized normally, so the URL reported is illegal. mistake.

There are some differences between accessing the URL in iOS and direct access from the browser. The browser will encode the URL with special characters when requesting it. However, when developing iOS, you need to encode it yourself. There are ready-made methods for NSString encoding, as follows

- (NSString *)URLEncodedString:(NSString*)resource {
    CFStringRef url = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)resource, NULL, CFSTR("!*'();:@&=+$,/?%#[]"), kCFStringEncodingUTF8); // for some reason, releasing this is disasterous
    NSString *result = (__bridge NSString *)url;
    //    [result autorelease];
    return result;
}

Please encode the NSString address before assembling NSURL.
This will convert the special characters !*'();:@&=+$,/?%#[] in the URL into the corresponding % format encoding, so that normal requests can be made.

Hope it helps and wish you good luck.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template