2

I am trying to send some key-value to one of my page (php page) using AFNetworking. I am using AFHTTPRequestOperationManager for this operation, the post is happening successfully however I am receiving blank values on server side.

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"name":@"1234",                               @"email":@"1234",@"regId":@"rty2345",@"device_flag":@"2"};
NSLog(@"%@", params);
manager.responseSerializer = [AFJSONResponseSerializer serializer]; // if response JSON format
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager POST:@"http://xyz.in/app/apns/register.php" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [self databaseInsertion];
    ShowViewController *mapp = [[self storyboard]instantiateViewControllerWithIdentifier:@"showview"];
    [self.navigationController pushViewController:mapp animated:YES];
    NSLog(@"%@", responseObject);
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}];

I am sending back a JSON string so that success block will be performed. But when I try to fetch the value at the server side, all values shows blank.

Then I tried to send data using NSMutableURLRequest *request which is also making same result, when I try to use the browser to send data like http://www.xyz.in/app/apns/register.php?name=15&email=1234&regId=r5678d&device_flag=2, it works well.

My Code using NSMutableURLRequest is below

 NSString *post =[[NSString alloc] initWithFormat:@"name=%d&email=%d&regId=%@&device_flag=%d", status,[_emailTextField.text intValue],@"r5678d",2];
 NSLog(@"%@",post);
 NSURL *url=[NSURL URLWithString:@"http://xyz.in/app/apns/register.php"];
 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
 NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
 [request setURL:url];
 [request setHTTPMethod:@"POST"];
 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
 [request setHTTPBody:postData];
 NSError *error;
 NSURLResponse *response;
 NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
 NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
halfer
  • 19,824
  • 17
  • 99
  • 186
Saty
  • 2,563
  • 3
  • 37
  • 88

1 Answers1

0

I don't know what was wrong in the post however I just cut and paste the code from a previous project and it worked like a charm.

NSString *post =[[NSString alloc] initWithFormat:@"name=%d&email=%d&regId=%@&device_flag=%d", status, [[_emailTextField text] intValue],app.deviceTooken,2];
NSURL *url=[NSURL URLWithString:@"http://www.xyz.in/app/apns/register.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
 NSError *error;
 NSURLResponse *response;
 NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
 NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
 NSLog(@"Data is %@",data);

However I still have doubt in AFHTTPRequestOperationManager why the parameter is not reaching my php file. Do you have anything in mind?

Saty
  • 2,563
  • 3
  • 37
  • 88
  • I don't know however the same piece of code has started working now. However the response which I should get ( and I am getting in normal NSMutableURLRequest) is not coming using AFHTTPRequestOperationManager. Its always showing 0. – Saty Feb 28 '14 at 07:47