2011-10-03 9 views
8

私はAFNetworkingを使用しており、jsonのフィードバックが必要なポストリクエストを作成しています。以下のコードは動作しますが、私は2つの主な質問があります。 ActivityIndi​​cator Managerのリリースはどこですか? 2番目の質問はこのコードが正しいことです。新しいものです。ブロックと混同されるので、うまく動作しているかどうかを知りたいのです。AFNetworkingポストリクエストwith json feedback

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:成功事例の:仕上げ:'メソッドから来る? APIでは表示されません。 –

+0

@reakinator彼は実際に '+ JSONRequestOperationWithRequest:success:failure:'を参照してください。[ここ](https://github.com/AFNetworking/AFNetworking#readme)を参照してください。 – borisdiakur

答えて

2

なぜ、代わりにこれを使用していませんか?

[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; 

はそのためのallocとinit

他のコードの多くは、単に客観-CとAFNetworkingを学び始めたと言うことはできません。.. :)

よろしく、 Steve0hhする必要はありません

8

私はこの質問が少し古いことを知っていますが、私はまだ貢献したいと思います。

steveOhhによると、[[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]を使用して、アクティビティネットワークインジケータをオンにする必要があります。これはsingletonなので、手動で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が正しいことを確認してください、NSWで失敗ブロックから 'NSError'をロギングして出力を参照することをお勧めします – ArturoVM

+0

@ Malekまだ問題を特定できない場合は、NSLogの出力で新しい質問を開いてください。 – ArturoVM