【问题标题】:How to pass the multiple values to the server using NSURLSESSION in Objective c [closed]如何在Objective c中使用NSURLSESSION将多个值传递给服务器[关闭]
【发布时间】:2015-12-14 08:05:51
【问题描述】:

我想将多个值传递给我的服务器,例如 1.phonenum,2.name 3.details 4.email.etc...

我的服务器使用 PHP。我想根据字段的键值将数据传递给。

电话号码 U_pnum:123456789 姓名 U_name:naveen 详细信息 U_details:开发者电子邮件 U_email:xyz@gmail.com 。

我想使用 NSUrlSession 发送数据。 我写了一些代码......但我没有从我的服务器得到任何响应。

请帮帮我。

谢谢你。

【问题讨论】:

  • 请发布您的php代码和目标c代码。
  • 为什么我在 google chrome 中使用 REST CLIENT ..我得到的响应就像成功更新了详细信息一样。比如{response,Yes}
  • 我的意思是,如果您不发布如何发送数据并且我认为您已成功将数据发送到您的服务器并且您的服务器有响应,我们如何修改您的代码以发送键值。
  • 对不起@jameshwart lopez 我没有 PHP 代码。他(php 开发人员)向我发送该字段的唯一键值。我正在处理我的目标 c 代码现在检查这个。 NSURLSession *session=[NSURLSession sharedSession]; NSString *url=[NSString
  • 我明白了。只需发布一些你的目标 c 代码。

标签: php objective-c iphone ios9 xcode7


【解决方案1】:

试试这个

NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];



     NSURL * url = [NSURL URLWithString:@"http://yourServerUrl/postFile.php"];
        NSMutableURLRequest * urlRequest = [NSMutableURLRequest requestWithURL:url];

        NSString * params =@"U_pnum=valueofU_pnum&U_name=valueofU_name&U_details=valueofU_details&U_email=valueofU_email";
        [urlRequest setHTTPMethod:@"POST"];
        [urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

        NSURLSessionDataTask * dataTask =[defaultSession dataTaskWithRequest:urlRequest
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                              NSLog(@"Response:%@ %@\n", response, error);
                                              if(error == nil)
                                              {
                                                  NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
                                                  NSLog(@"Data = %@",text);
                                              }

                                          }];
        [dataTask resume];

【讨论】:

  • 我收到类似这样的错误 2015-12-14 14:29:34.363 LCall[3718:1197217] 响应: { URL: 45254.552/index.php/webservices/mobile_login } { 状态代码: 200 ,标题{连接=关闭; “内容类型”=“文本/html”;日期 =“2015 年 12 月 14 日星期一 08:59:37 GMT”;服务器 = "Apache/2.4.16 (Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4"; “传输编码” = 身份; "X-Powered-By" = "PHP/5.5.30"; } } (null)
  • 2015-12-14 14:29:34.364 LCall[3718:1197217] Data = {"response":"no","error":"此用户名或密码不匹配"}
  • 我看到状态码是200表示请求成功
  • 你使用的服务器的响应是" {"response":"no","error":"This username and password mismatch"} "表示你发送的请求成功并且您已发送正确的密钥。
  • 谢谢这意味着我成功地更新了我的数据.. 进入服务器。 :P 但是.....我收到“没有回复”。我该如何解决这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
  • 2021-03-29
相关资源
最近更新 更多