我对AFNetworking v.1.x有下一个问题,可能与2.x
的问题相同#define LOGIN_URL @"http://myserverr.com/login"
NSURL *url = [NSURL URLWithString:LOGIN_URL relativeToURL:[NSURL URLWithString:LOGIN_URL]];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url ];
[httpClient postPath:nil
parameters:@{EMAIL_KEY : email,
PASSWORD_KEY : password}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
NSDictionary *result = (NSDictionary *)json;
[DCDDownloadHelper loginResult:result];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
dispatch_async(dispatch_get_main_queue(), ^{
}];
但是结果我的请求是url http://myserverr.com/login/而不是http://myserverr.com/login我在下一个文档中自动添加的最后一个斜杠
// Ensure terminal slash for baseURL path, so that NSURL +URLWithString:relativeToURL: works as expected
if ([[url path] length] > 0 && ![[url absoluteString] hasSuffix:@"/"]) {
url = [url URLByAppendingPathComponent:@""];
}
但它对我没有帮助:)。
答案 0 :(得分:1)
鉴于此行为,您可以将网址拆分为BASE_URL
和LOGIN_PATH
:
#define BASE_URL @"http://myserverr.com/"
#define LOGIN_PATH @"login"
NSURL *baseURL = [NSURL URLWithString:BASE_URL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[httpClient postPath:LOGIN_PATH
parameters:@{EMAIL_KEY : email,
PASSWORD_KEY : password}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// ...
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// ...
}];