【问题标题】:Passing array on Webservices在 Web 服务上传递数组
【发布时间】:2015-04-29 12:11:15
【问题描述】:

我正在开发一个应用程序,因为我曾经通过 WebServices 将我的联系号码发送到服务器。这里我获取我的联系号码一个数组,然后我通过 webservices 发送联系人数组。下面的代码,

NSString *myRequestString1 = [[NSString alloc] initWithFormat:@"contact_numbers=%@",MobiileArray ];


NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString1 UTF8String ] length: [ myRequestString1 length ] ];
NSMutableURLRequest *request = [ [ NSMutableURLRequest alloc ] initWithURL: [ NSURL URLWithString:@"http://myproject.in/myproject-contacts.php"]];

[request setHTTPMethod: @"POST"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[request setHTTPBody: myRequestData];
NSURLResponse *response;
NSError *err;
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSError* error;
NSMutableArray* result = [NSJSONSerialization JSONObjectWithData:returnData
                                                         options:kNilOptions

                                                           error:&error];
NSLog(@"result: %@", result);

但响应为空,我的代码有错误吗?可以给我推荐一下吗谢谢。

【问题讨论】:

  • 你遇到了什么错误?
  • 这听起来很傻,但你确定你的问题出在客户端吗?也许您的网络服务没有返回您所期望的。
  • 是的,我通过 POST 和 GET 方法做了很多服务,但在这里我很挣扎@Zil

标签: ios objective-c iphone xcode nsmutableurlrequest


【解决方案1】:

试试下面的解决方案..!

替换这行代码

NSString *myRequestString1 = [[NSString alloc] initWithFormat:@"contact_numbers=%@",MobiileArray ];
NSData *myRequestData = [ NSData dataWithBytes: [ myRequestString1 UTF8String ] length: [ myRequestString1 length ] ];

//有了这行代码

//NSDictionary *dict = [NSDictionary dictionaryWithObject: MobiileArray forKey:@"contact_numbers"];

NSString* myRequestString1 = [MobiileArray JSONRepresentation];
NSData *myRequestData = [myRequestString1 dataUsingEncoding:NSUTF8StringEncoding];

//放

[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

检查一下...

【讨论】:

  • 谢谢,contact_numbers不是key,是发送数据到webservices的参数
  • 我修改了答案...现在检查...!
  • 同样的问题,它正在传递 null @vidhyanand900
  • 代码中的contact_numbers是什么...是传递ContactArray的参数/键?键与参数相同...!
  • contact_numbers 是一个网络服务参数
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-12
相关资源
最近更新 更多