2016-08-11 17 views
0

私はAFNetworkingを使ってWebにデータを投稿しています。私が持っている情報はURIです。baseURL/post_user_infoです。inputとなります。それぞれの名前と値のペアを含むJSONオブジェクト。以下のコードでは、名前と値のペアを辞書に設定しました。私の質問はそれをjsonにして入力値として送る方法です。POSTリクエストでJSONオブジェクトを送信するAFNetworking、iOS

NSString *string = [NSString stringWithFormat:@"%@?post_user_info",BaseURLString]; 
NSString *escapedPath = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 

    NSDictionary *params = @{@"input_1": @"hello world", 
          @"input_2": @"[email protected]", 
          @"input_3": @"newworldorder"}; 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:params options:kNilOptions error:nil]; 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setHTTPBody:jsonData]; 

[manager POST:escapedPath parameters:[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] progress:nil success:^(NSURLSessionTask *task, id responseObject) 
{ 
    NSLog(@"%@",responseObject); 
}failure:^(NSURLSessionTask *operation, NSError *error) 
{ 
    NSLog(@"%@",[error localizedDescription]); 
}]; 

今私はJSONを生成しているので、私は、コードを更新しましたが、私は今のコードを実行した場合、それは私にエラーを与える:、不正な要求エラーが出力されますinput_valuesが空の場合は意味400を。

答えて

2

@interface AFJSONRequestSerializer:AFHTTPRequestSerializer

AFJSONRequestSerializer is a subclass of AFHTTPRequestSerializer that encodes parameters as JSON using NSJSONSerialization .

は、送信JSON生データは、これを追加し、一度

manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 

例えば

NSString *string = [NSString stringWithFormat:@"%@?post_user_info",BaseURLString]; 
NSString *escapedPath = [string stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; 

/************** karthik Code added ******************/ 
AFHTTPSessionManager* manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
manager.responseSerializer = [AFHTTPResponseSerializer serializer]; 
manager.requestSerializer = [AFJSONRequestSerializer serializer]; 
     /************** karthik Code added ******************/ 

    NSDictionary *params = @{@"1": @"hello world", 
          @"2": @"[email protected]", 
          @"3": @"newworldorder"}; 
[manager POST:escapedPath parameters:params progress:nil success:^(NSURLSessionTask *task, id responseObject) 
    { 
     NSLog(@"%@",responseObject); 
      NSError *error = nil; 
     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]; 
      NSLog(@"json == %@", json); 

    } failure:^(NSURLSessionTask *operation, NSError *error) { 
     NSLog(@"%@", [error localizedDescription]); 
    }]; 
を試していないため、この AFJSONRequestSerializerが使用されている追加

更新のparams

NSDictionary *params = @{@"1": @"hello world", 
         @"2": @"[email protected]", 
         @"3": @"newworldorder"}; 
NSMutableDictionary *modify = [NSMutableDictionary new]; 
[modify setObject:params forKey:@"input_values"]; 

あなたはちょっと

enter image description here

+0

の出力を得ます!正常な応答ではありません<7b227374 61747573 223a3430 302c2272 6573706f 6e736522 3a224261 64207265 71756573 74227d> ''これは、それが今の成功だ...素晴らしい作品...しかし、レスポンスオブジェクトです。間違った値を辞書に入れているからですか?どこを探しているのですか? –

+0

@ TalhaCh - すべてが正しいです、 'responseObject'はデータを今すぐserlizeする必要があるデータですので、** Success **ブロック –

+0

で更新された答えを確認してくださいあなたはこれを試していますか{ " input_values ":{ " input_1 「: "こんにちは"、 "input_2_3": "ジョン"、 "input_2_6": "スミス"、 "input_4": "マイ段落" } } –

関連する問題