【问题标题】:How to post data in url using json?如何使用json在url中发布数据?
【发布时间】: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 并单击按钮时,我的表格上的数据没有更新。
  • 为什么你使用两种方法来后期使用任何一种
  • 谢谢先生,但我已经评论了我的第一个发布方法。
  • 先生,请问您是否可以编辑我的代码。

标签: ios iphone json xcode


【解决方案1】:
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);

【讨论】:

  • 您好先生,我遵循了您的代码,但在调试器上出现错误:结果:(null)。
  • initWithObjectsAndKeys内部,键和值的顺序应该是相反的。值必须在键之前,如方法名称所示。
【解决方案2】:

试试我创建的 gist 中的代码

使用PAW 生成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-17
    • 2015-05-05
    • 2018-05-28
    • 2011-02-24
    • 2011-09-09
    • 1970-01-01
    相关资源
    最近更新 更多