AFNetworking出现-1002解决办法

Error Domain=NSURLErrorDomain Code=-1002 "不支持的 URL" 
UserInfo={NSUnderlyingError=0x17425f4a0
{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}

一般此问题对URL编码就可以了,但是如果用get请求,参数有空格,中文等字符时,需要对参数进行编码

可以在AFNetworking入口参数处进行编码

NSString * AFQueryStringFromParameters(NSDictionary *parameters) {
    NSMutableArray *mutablePairs = [NSMutableArray array];
    for (AFQueryStringPair *pair in AFQueryStringPairsFromDictionary(parameters)) {
        [mutablePairs addObject:[pair URLEncodedStringValue]];
    }
    NSString *queryString = [mutablePairs componentsJoinedByString:@"&"];
    queryString = [queryString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    return queryString;
}

也可以在入口URL这里进行编码
在这个方法里

- (NSURLRequest *)requestBySerializingRequest:(NSURLRequest *)request
                               withParameters:(id)parameters
                                        error:(NSError *__autoreleasing *)error

对URL进行编码

if (query && query.length > 0) {
    NSString *urlString = [[mutableRequest.URL absoluteString] stringByAppendingFormat:mutableRequest.URL.query ? @"&%@" : @"?%@", query];
    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    mutableRequest.URL = [NSURL URLWithString: urlString];
}

你可能感兴趣的:(AFNetworking出现-1002解决办法)