2013-11-26 45 views
2

末我有一個問題與AFNetworking v.1.x因爲這可能是同樣的問題,與2.x的AFNetworking削減在URL中自動添加

#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

你就不能使用'NSURLConnection'呢? – theShay

+0

和..也許這可以幫助你:http://github.com/AFNetworking/AFNetworking/issues/994 – theShay

回答

1

鑑於這種行爲,您可以將網址分成BASE_URLLOGIN_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) { 
       // ... 
      }]; 
+0

它真的幫助我)謝謝 –