【发布时间】:2015-10-28 09:12:11
【问题描述】:
这是我在 url 中发布数据的代码。
- (IBAction)Register:(id)sender {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=registration"]];
[request setHTTPMethod:@"POST"];
NSLog(@"the company name is:%@",_CompanyName.text);
NSLog(@"the email is:%@",_Email.text);
NSLog(@"the password is:%@",_Password.text);
NSLog(@"the password again is:%@",_Passwordagin.text);
NSString *strParameters =[NSString stringWithFormat:@"email_id=%@&company_name=%@&password=%@",_Email.text,_CompanyName.text,_Password.text, nil];
NSLog(@"the data Details is =%@", strParameters);
NSData *data1 = [strParameters dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data1];
// NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//Handle your response here
//}];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
NSLog(@"got response");
}
else
{
NSLog(@"faield to connect");
}
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[http://dev1.brainpulse.org/quickmanhelp/webservice/api.php?act=registration"];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request1 addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request1 addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request1 setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"company_name", _CompanyName.text,
@"email_id", _Email.text,@"password", _Password.text,
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//Handle your response here
}];
[postDataTask resume];
【问题讨论】:
-
你遇到了什么问题
-
当输入所有 uitextfield 并单击按钮时,我的表格上的数据没有更新。
-
为什么你使用两种方法来后期使用任何一种
-
谢谢先生,但我已经评论了我的第一个发布方法。
-
先生,请问您是否可以编辑我的代码。