我想發送json數組數據到服務器,但它顯示結果失敗。 所以plaese糾正我在哪裏,我沒有wrong.Here是代碼我正在使用本:在iOS中發佈json數組到服務器?
NSDictionary *[email protected]{@"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連接委託方法調用,但響應顯示爲零。 所以請檢查並糾正我。提前感謝。
您的問題已經在這裏找到答案:http://stackoverflow.com/questions/7673127/how-to-send-post-or-get-request-containing-json-to-a-url –
我也在使用它,但沒有使用,結果是:requestReply:{「result」:「0」} –