如何使用json在url中发布数据?

时间:2015-10-28 09:12:11

标签: ios iphone json xcode

  

这是我在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];

this is table structure of my database.

2 个答案:

答案 0 :(得分:0)

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];

NSURLResponse *response = nil;
NSError *error = nil;

NSData *bodyData = [NSJSONSerialization dataWithJSONObject:mapData options:NSJSONWritingPrettyPrinted error:&error];
[request setHTTPBody:bodyData];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *strResponce = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSMutableDictionary *dictResponse = [NSJSONSerialization JSONObjectWithData:[strResponce dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&error];
NSLog(@"Result: %@",dictResponse);

答案 1 :(得分:0)

尝试我创建的gist

中的代码

使用PAW生成的内容。