2012-07-02 11 views
18

EDIT 07/14AFNetworking - POSTリクエスト

ビル・バージェスは、彼の答えのコメントでmentionnedように、この質問はAFNetworkingversion 1.3に関連しているにする方法。ここの新人のために時代遅れかもしれません。


私はiPhoneの開発に非常に新たなんだ、と私は私のサービスライブラリとしてAFNetworkingを使用しています。

私が照会しているAPIはRESTful APIであり、POSTリクエストを行う必要があります。これを行うために、私は次のコードで試してみました:

  • AFJSONRequestOperationGET要求、ないPOSTものを作るようだ:

    NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; 
    NSURL *url = [NSURL URLWithString:@"http://localhost:8080/login"]; 
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
        NSLog(@"Pass Response = %@", JSON); 
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
        NSLog(@"Failed Response : %@", JSON); 
    }]; 
    [operation start]; 
    

    このコードを持つ2つの主要な問題があります。

  • このメソッドにパラメータを設定することはできません。

また、私はこのコードを試してみました:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:@"my_username", @"username", @"my_password", @"password", nil]; 
NSURL *url = [NSURL URLWithString:@"http://localhost:8080"]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 

[httpClient postPath:@"/login" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    NSLog(@"Succes : %@", responseObject); 
} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Failure : %@", error); 
}]; 

は、私はそれを成し遂げるためにここに欲しいものを作るために良い方法はありますか?

ありがとうございました!

答えて

24

AFNetworkingで使用されているリクエストのデフォルト動作を、POSTとして処理することができます。

NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil]; 

これは、カスタムクライアントを使用するデフォルトAFNetworking設定を上書きしている前提としています。あなたがいない場合、私はそれを行うことをお勧めします。ネットワーククライアントを処理するためのカスタムクラスを作成するだけです。

MyAPIClient.h

#import <Foundation/Foundation.h> 
#import "AFHTTPClient.h" 

@interface MyAPIClient : AFHTTPClient 

+(MyAPIClient *)sharedClient; 

@end 

MyAPIClient.m

@implementation MyAPIClient 

+(MyAPIClient *)sharedClient { 
    static MyAPIClient *_sharedClient = nil; 
    static dispatch_once_t oncePredicate; 
    dispatch_once(&oncePredicate, ^{ 
     _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:webAddress]]; 
    }); 
    return _sharedClient; 
} 

-(id)initWithBaseURL:(NSURL *)url { 
    self = [super initWithBaseURL:url]; 
    if (!self) { 
     return nil; 
    } 
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 
    [self setDefaultHeader:@"Accept" value:@"application/json"]; 
    self.parameterEncoding = AFJSONParameterEncoding; 

    return self; 

} 

その後、あなたは何の問題もなく動作キュー上のネットワーク呼び出しをオフに解雇することができるはずです。

MyAPIClient *client = [MyAPIClient sharedClient]; 
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES]; 
    [[AFNetworkActivityIndicatorManager sharedManager] incrementActivityCount]; 

    NSString *path = [NSString stringWithFormat:@"myapipath/?value=%@", value]; 
    NSURLRequest *request = [client requestWithMethod:@"POST" path:path parameters:nil]; 

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 
     // code for successful return goes here 
     [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount]; 

     // do something with return data 
    }failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 
     // code for failed request goes here 
     [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount]; 

     // do something on failure 
    }]; 

    [operation start]; 
+0

返信いただきありがとうございます。ただ一つの質問:あなたの 'SIOAPIClient'は 'MyAPIClient'でしょうか? –

+0

あなたは正しいです...私は自分の実装からこれをコピー/ペーストし、クラス名を変更しました。私が推測するスポットを逃した。どのようにするべきか私の答えを編集しました。 –

+0

私はあなたが書いているようにカスタムクライアントを実装しようとしましたが、エラーが発生しました: 'セレクタ 'sharedClient''のための既知のクラスメソッドがありません。ヘッダーが正しく組み込まれているので、なぜこのエラーが発生しているのだろうかと思います。 –