2013-08-20 15 views
6

POSTを介してサーバーにパラメータを送信しようとしていますが、一般的には動作しますが、配列を含むJSONをパラメータの1つとして送信する方法がわかりません。ここで私が試したことは次のとおりです。AFNetworkingはPOST要求のJSONパラメータで配列を送信します

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:myURL]]; 
NSMutableArray *objectsInCart = [NSMutableArray arrayWithCapacity:[_cart count]]; 
for(NSDictionary *dict in _cart) 
{ 
    NSObject *object = [dict objectForKey:@"object"]; 
    NSDictionary *objectDict = @{@"product_id": [NSString stringWithFormat:@"%d",[object productID]], 
           @"quantity": [NSString stringWithFormat:@"%d", [[dict objectForKey:@"count"] intValue]], 
           @"store_id": [NSString stringWithFormat:@"%d", [Store getStoreID]], 
           @"price": [NSString stringWithFormat:@"%.2f", [object price]]}; 
    [objectsInCart addObject:objectDict]; 
} 
NSError *error = nil; 
NSString *cartJSON = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart 
                        options:NSJSONWritingPrettyPrinted 
                         error:&error] 
              encoding:NSUTF8StringEncoding]; 

if(error) 
{ 
    NSLog(@"Error serializing cart to JSON: %@", [error description]); 
    return; 
} 

NSDictionary *parameters = @{@"status": @"SUBMITTED", 
          @"orders": cartJSON}; 

NSMutableURLRequest *orderRequest = [httpClient requestWithMethod:@"POST" 
                  path:@"/app/carts" 
                 parameters:parameters]; 

AFJSONRequestOperation *JSONOperation = [[AFJSONRequestOperation alloc] initWithRequest:orderRequest]; 

ただし、このJSONを送信するときにエラーが発生します。どんな提案も大歓迎です!

+0

サーバは何を期待しているのですか?しかし、通常、JSONのすべての項目は配列を含むキーを持っています。あなたはそれのためのキーなしで今すぐ配列を送信しているだけです。 "NSString * cartJSON = @" "products":%@ "、[[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:objectsInCart options:NSJSONWritingPrettyPrintedエラー:&error]エンコード:NSUTF8StringEncoding]を試してください。 – dirkgroten

+0

'parameters'ディクショナリを見ると、配列のキーは' @ "orders" ' – Mason

+0

です。あなたは実際に送られてきたデータを見ましたか?私はチャールズ・プロキシのようなアプリケーションを、私のアプリから外部サーバーへのすべてのトラフィックを傍受するために非常に価値があるようにしてきました。 – dirkgroten

答えて

9

あなたはJSONを投稿したい指定しているので、私はあなたがAFHTTPClientのドキュメントによると、このように書きフォームURLパラメータのエンコーディングを、送っている賭けているところ私は表示されません。

If a query string pair has a an NSArray for its value, each member of the array will be represented in the format field[]=value1&field[]=value2 . Otherwise, the pair will be formatted as "field=value". String representations of both keys and values are derived using the -description method. The constructed query string does not include the ? character used to delimit the query component.

サーバーが実際にそれをAFNetworking伝えるために、第2行目にこれを追加し、あなたはJSONを投稿することを期待されている場合:

// AFNetworking 1.0 
// httpClient is a subclass of AFHTTPClient 
httpClient.parameterEncoding = AFJSONParameterEncoding; 

// AFNetworking 2.0 
// httpClient is a subclass of AFHTTPRequestOperationManager or AFHTTPSessionManager 
httpClient.requestSerializer = AFJSONRequestSerializer; 

あなたはその後、NSJSONSerializationにあなたの呼び出しを削除し、ちょうどparameters DICにobjectsInCartを置きます。

サイドノート:それはAFHTTPRequestOperationManagerAFHTTPSessionManager(AFNetworking 2.0)またはAFHTTPClient(AFNetworking 1.0)をサブクラス化し、あなたのinitWithBaseURL:方法でこのタイプの構成を置くのが普通です。 (リクエストごとに新しいクライアントを作成したいとは思わないかもしれません)。

+0

ガーええ、私はこれを以前に見つけました、投稿を更新してください。ありがとう! – Mason

関連する問題