du1462 2015-09-17 03:13
浏览 29
已采纳

如何将数据发送到PHP服务器?

i`m having a problem doing this code to send a data to my php server can someone help me. this is my code that gives me a headache, this is my Outlets/ textfield.

@property (weak, nonatomic) IBOutlet UITextField *parentsNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *professionTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextField *contactTextField;
@property (weak, nonatomic) IBOutlet UITextField *emailAddressTextField;
@property (weak, nonatomic) IBOutlet UITextField *osTypeTextField;

and this is the Action Button

- (IBAction)registerButtonTapped:(id)sender {


 NSString *post = [NSString stringWithFormat:@"&parentNameTextField=%@&professionTextField=%@",@"parentNameTextField",@"professionTextField"];



NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];




[request setURL:[NSURL URLWithString:@"http://a7c8a2a.ngrok.com/usermanagement_dummy/user/register"]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (conn)
{
    NSLog(@"Connection Success");

}
else
{
    NSLog(@"Connection could not be made");

}

}

im trying my best to continue but i dont know how to. any help would be gladly appreciate.

  • 写回答

2条回答 默认 最新

  • dtn43447 2015-09-17 03:21
    关注
    NSMutableDictionary *get = [[NSMutableDictionary alloc]init];
    [get setObject:@"parentNameTextField" forKey:@"parentNameTextField"]; 
    [get setObject:@"parentNameTextField" forKey:@"professionTextField"]; 
    
    
    if([NSJSONSerialization isValidJSONObject:get]){
    
    
    //convert object to data
    NSData* jsonData = [NSJSONSerialization dataWithJSONObject:get options:kNilOptions error:nil];
    
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://a7c8a2a.ngrok.com/usermanagement_dummy/user/register"]];
    
    
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setHTTPBody:jsonData];
    
    
    NSURLSessionConfiguration *config=[NSURLSessionConfiguration defaultSessionConfiguration];
    
    NSURLSession *session=[NSURLSession sessionWithConfiguration:config];
    
    NSURLSessionDataTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
        if(response){
            NSString *resp = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
            NSLog(@"Echo %@",resp);
    
    
        }
    
        else{
            NSLog(@"Timeout");
    
        }
    
    
    
    }];
    [task resume];
    
    
    }
    

    Make a dictionary of key value pair.Then create q request and NSURLSessionDataTask to POST your parameters.In the completion handler code u can get response also u can add error handling in dataTaskWithRequest completion handler Try the below code.This will work for you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab