简体   繁体   中英

How to send json data in the Http request using AFNetworking

I have to pass data like json format to the server

{"email":"abc@gmail.com","password":"abc"}

and i have used this code but data is not pass to the server please help me..

    NSDictionary *dict1=@{@"email": @"biren123@gmail.com"};
    NSDictionary *dict2=@{@"password": @"biren"};
    services *srv=[[services alloc]init];

    NSString *str=@"http://emailsending.in/setting_saver_api/";
    NSString *method=@"login.php";
    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

    [dict setValue:dict1 forKey:@"email"];

    [dict setValue:dict2 forKey:@"password"];


    [srv postToURL:str withMethod:method andParams:dict completion:^(BOOL success, NSDictionary *responseObj)

     {
         NSLog(@"res :%@",responseObj);
         NSLog(@"%d",success);

         NSLog(@"Successfully..................");


     }];

you can check your link at https://www.hurl.it/

1)give your http link

2)select your method type "Get" or "Post"

3)add the parameters

and check the response.If you get the same response contact your backend team.

Try this. 1) Convert your dictionary into json using NSJSONSerialization and store it in NSdata.

NSData * incidentData = [NSJSONSerialization dataWithJSONObject: params options:0 error:&err];

2) Post the NSdata containing json to your server

Try this,if this helps

  NSString *str = @"http://emailsending.in/setting_saver_api/"; 

NSString*method = @"login.php";

    services *srv=[[services alloc]init];

    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary]; [dict1 setValue:@"biren123@gmail.com" forKey:@"email"];

    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary]; [dict2 setValue:@"biren" forKey:@"password"];


    NSMutableDictionary *dict=[[NSMutableDictionary alloc]init];

        [dict setValue:dict1 forKey:@"email"];

        [dict setValue:dict2 forKey:@"password"];

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer = [AFJSONResponseSerializer serializer];

    [manager POST:str parameters:dict success:^(NSURLSessionDataTask
    *task, id responseObject) {

            NSLog(@"JSON: %@", responseObject);
            //here is place for code executed in success case

    } failure:^(NSURLSessionDataTask *task, NSError *error) {

            //here is place for code executed in error case
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error while sending POST"
                                                                message:@"Sorry, try again."
                                                               delegate:nil
                                                      cancelButtonTitle:@"Ok"
                                                      otherButtonTitles:nil];
            [alertView show];

            NSLog(@"Error: %@", [error localizedDescription]); }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM