8

AFNetworking を使用して、json フィードバックが必要な投稿リクエストを作成しています。以下のコードは機能しますが、主な質問が 2 つあります。ActivityIndi​​cator Manager はどこでリリースしますか? 2 番目の質問は、このコードが正しいかどうかです。新しいので、ブロックと混同してしまうので、最適なパフォーマンスのために正しいことを行っているかどうかを本当に知りたいと思っています。

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


}    

事前にご協力いただきありがとうございます:)

4

2 に答える 2

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
于 2012-01-19T03:11:55.933 に答える
2

代わりにこれを使ってみませんか?

    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];

したがって、割り当てて初期化する必要はありません

他のコードについてはあまり言えません。Objective-CとAFNetworkingの学習を始めたばかりです。:)

よろしく、Steve0hh

于 2011-10-09T16:40:20.583 に答える