我正在尝试使用AFNetworking将一些键值发送到我的一个页面(php页面)。我正在使用AFHTTPRequestOperationManager进行此操作,发布成功但是我在服务器端收到空值。
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];
}];
我发回一个JSON字符串,以便执行成功阻止。但是当我尝试在服务器端获取值时,所有值都显示为空白。
然后我尝试使用NSMutableURLRequest *请求发送数据,这也是同样的结果,当我尝试使用浏览器发送像http://www.xyz.in/app/apns/register.php?name=15&email=1234®Id=r5678d&device_flag=2这样的数据时,它运行良好。
使用NSMutableURLRequest的我的代码低于
NSString *post =[[NSString alloc] initWithFormat:@"name=%d&email=%d®Id=%@&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];
答案 0 :(得分:0)
我不知道帖子里有什么问题但是我只是剪切并粘贴了之前项目中的代码,它就像一个魅力。
NSString *post =[[NSString alloc] initWithFormat:@"name=%d&email=%d®Id=%@&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);
但是我仍然怀疑AFHTTPRequestOperationManager为什么参数没有到达我的php文件。你有什么想法吗?