2011-10-03 138 views
8

我正在使用AFNetworking並創建一個需要json反饋的post請求。下面的代碼工作,但我有兩個主要問題;我在哪裏可以發佈ActivityIndi​​cator Manager?第二個問題是這段代碼是正確的,是新的,我和塊混淆了,所以我真的想知道,如果我爲最佳性能做正確的事情,即使它有效。AFNetworking Post與json反饋請求

NSURL *url = [NSURL URLWithString:@"mysite/user/signup"]; 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

    AFNetworkActivityIndicatorManager * newactivity = [[AFNetworkActivityIndicatorManager alloc] init]; 
    newactivity.enabled = YES; 
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: 
          usernamestring, @"login[username]", 
          emailstring, @"login[email]", 
          nil]; 
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:@"mysite/user/signup"parameters:params]; 
    [httpClient release]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation operationWithRequest:request success:^(id json) { 

     NSString *status = [json valueForKey:@"status"]; 
     if ([status isEqualToString:@"success"]) { 
      [username resignFirstResponder]; 
      [email resignFirstResponder]; 
      [self.navigationController dismissModalViewControllerAnimated:NO]; 
     } 
     else { 
      UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful" 
                  message:@"Please try again" 
                  delegate:NULL 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:NULL]; 

      [alert show]; 
      [alert release]; 
     } 

    } 

    failure:^(NSHTTPURLResponse *response, NSError *error) { 

    NSLog(@"%@", error); 
    UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"Login Unsuccessful" 
                 message:@"There was a problem connecting to the network!" 
                 delegate:NULL 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:NULL]; 

     [alert show]; 
     [alert release]; 


    }]; 

    NSOperationQueue *queue = [[[NSOperationQueue alloc] init] autorelease]; 
    [queue addOperation:operation]; 
    NSLog(@"check");  


}  

非常感謝您的幫助提前:)

+0

哪裏了'AFJSONRequestOperation operationWithRequest:sucess:面漆:'方法來自?我沒有在API中看到它。 –

+0

@reakinator他實際上'+ JSONRequestOperationWithRequest:成功:失敗:'見示例[這裏](https://github.com/AFNetworking/AFNetworking#readme)。 – borisdiakur

回答

2

爲什麼不使用這個呢?

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; 

因此沒有必要的alloc和init

不能說太多的其他代碼,剛開始時學習Objective-C和AFNetworking .. :)

問候, Steve0hh

8

我知道這個問題有點老,但我仍然想貢獻。

正如steveOhh所說,您應該使用[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]來打開活動網絡指示器。它是一個singleton,因此它不需要你手動alloc-init和release。至於其他的問題,我注意到你缺少一些參數在塊調用,也可以做到這一點,這是非常乾淨的代碼:

NSURL *url = [NSURL URLWithString:@"mysite/user/signup"]; 
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:[NSURLRequest requestWithURL:url] success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
    // your success code here 
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
    // your failure code here 
}]; 

[operation start]; // start your operation directly, unless you really need to use a queue 
+0

你好,我用你的代碼,但我總是進入失敗塊:(我失去了什麼嗎? – Luca

+0

@Malek檢查你的URL是否正確,我推薦NSLogging從失敗塊中的'NSError'並看到輸出 – ArturoVM

+0

@ Malek如果您仍然無法確定問題,請使用該NSLog的輸出打開一個新問題,我很樂意爲您提供幫助。 – ArturoVM