iOS - NSURLConnection finished with error - code -1002

 

网络请求会异常,比如报错:Error domain: @"NSURLErrorDomain" - code: 1002

在回调的地方打一个断点,然后用命令行查看下什么错误: po error
当错误是:**[Error Domain=NSURLErrorDomain Code=-1002 ] "unsupported URL" **
*Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo={NSLocalizedDescription=unsupported URL, NSUnderlyingError=0x7fa9b1d06120 {Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}}  *  的时候是url编码问题,处理一下URL编码就好了

我这边是后台返回的拼接的接口多了“/”,导致统一的域名格式不统一

http://gwtest.opg-iot.com:8000//api-sdeventApplication/images/handle/event_result/event_result_beimg_20190522093224_0.jpeg?Authenticator=eyJhbGciOiJIUzI1NiJ9.eyJyZWFsTmFtZSI6bnVsbCwibG9naW5OYW1lIjoiU0Q3MzkzIiwidXNlclR5cGUiOiIxIiwicGxhdGZvcm1JZCI6MTAsImV4cCI6MTU1OTA5MzkxOSwidXNlcklkIjo3MzkzfQ.KieGb8LUsgpvn9BU9jjSiNSNn12irclYKiPH_s0Uqgk

 

 

处理方法:


iOS 8 要用utf8转码:

urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];


iOS 8 以后要用这个方法:

urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"#%^{}\"[]|\\<> "].invertedSet];

 

转载于:https://www.cnblogs.com/gongyuhonglou/p/10904100.html

你可能感兴趣的:(iOS - NSURLConnection finished with error - code -1002)