Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/103.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS中将json数组发布到服务器?_Ios_Objective C_Json - Fatal编程技术网

在iOS中将json数组发布到服务器?

在iOS中将json数组发布到服务器?,ios,objective-c,json,Ios,Objective C,Json,我试图将json数组数据发送到服务器,但结果显示失败。 请纠正我哪里做错了。下面是我在这里使用的代码: NSDictionary *dict=@{@"groupmembersarray":contactsArray}; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPre

我试图将json数组数据发送到服务器,但结果显示失败。 请纠正我哪里做错了。下面是我在这里使用的代码:

NSDictionary *dict=@{@"groupmembersarray":contactsArray};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
                                                    options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *post = [NSString stringWithFormat:@"%@",jsonString];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:@"http://anaadit.net/caffe/newapp/AddGroupContact.php"]];
[request setHTTPMethod:@"POST"];

[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn==nil) {
    NSLog(@"Connection could not be made");
} else {

    responseData = [NSMutableData new];
    NSLog(@"%@",responseData);
}
和nsurl连接委托方法调用,但响应显示为nil。
因此,请检查并纠正我。提前谢谢。

请联系您的Web服务开发人员,他/她可能有一些问题。

您的问题已在此处得到回答:

尽管从您的评论判断,问题不在于实际的请求,而在于服务器。如果您的请求代码有错误,您就不会得到响应。您应该检查参数是否与服务器所需的一致

您还可以检查请求的状态代码,如下所示:

[(NSHTTPURLResponse*)response statusCode]

使用AFNetworking框架,易于使用和实现。

AFN网络中的POST请求-

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *dict=@{@"groupmembersarray":contactsArray};
 manager.requestSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"http://anaadit.net/caffe/newapp/AddGroupContact.php" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
要设置您可以使用的ant http头字段

[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

您的问题已经在这里得到了回答:我也在使用它,但没有使用结果是:requestReply:{“result”:“0”}