在Webservices上传递数组

时间:2015-04-29 12:11:16

标签: ios objective-c iphone xcode nsmutableurlrequest

我正在开发一个应用程序,因为我曾经通过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);

但响应为空,我的代码有任何错误?你能建议我谢谢你。

1 个答案:

答案 0 :(得分:0)

尝试以下解决方案..!

替换这行代码

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

// put

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

检查......