Iphone 带有json反馈的AFPOST请求

Iphone 带有json反馈的AFPOST请求,iphone,json,post,afnetworking,Iphone,Json,Post,Afnetworking,我正在使用AFNetworking并创建一个post请求,我需要json反馈。下面的代码有效,但我有两个主要问题;在哪里发布ActivityIndicator Manager?第二个问题是,这段代码是正确的,因为是新代码,我对代码块感到困惑,所以我真的想知道我是否做了正确的事情以获得最佳性能,即使它可以工作 NSURL *url = [NSURL URLWithString:@"mysite/user/signup"]; AFHTTPClient *httpClient = [[

我正在使用AFNetworking并创建一个post请求,我需要json反馈。下面的代码有效,但我有两个主要问题;在哪里发布ActivityIndicator Manager?第二个问题是,这段代码是正确的,因为是新代码,我对代码块感到困惑,所以我真的想知道我是否做了正确的事情以获得最佳性能,即使它可以工作

    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");    


}    

非常感谢您事先提供的帮助:)

为什么不改用这个呢

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
因此,不需要alloc和init

关于其他代码没什么可说的,刚开始学习objective-C和AFNetworking:)

问候,,
Steve0hh

我知道这个问题有点老了,但我还是想有所贡献

正如steveOhh所说,您应该使用
[[AFNetworkActivityIndicatorManager sharedManager]setEnabled:YES]
打开活动网络指示器。它是一个函数,因此不需要手动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

AFJSONRequestOperation操作withrequest:suces:finish:
方法来自哪里?我在API中看不到它。@reakinator他实际上使用了
+JSONRequestOperationWithRequest:success:failure:
参见示例。您好,我使用了您的代码,但我总是进入故障块:(我遗漏了什么吗?@Malek请检查您的URL是否正确,我建议从故障块中记录
NSError
,并查看输出。@Malek如果您仍然无法确定问题,请使用该NSLog的输出打开一个新问题,我将很乐意帮助您。@ArturoVM我正在使用您的代码,我正在徘徊在哪里设置方法od作为邮寄?谢谢