我的Json post方法在ios中不起作用?

时间:2014-07-29 05:34:31

标签: ios objective-c json http-post

我正在尝试使用JSON POST方法将一些数据发布到网络服务,我尝试了很多方法来做到这一点,但没有一种方法可行。这是我的代码,请检查:

NSArray *keys = [NSArray arrayWithObjects:@"subject", @"userID", @"message", nil];
NSArray *objects = [NSArray arrayWithObjects:@"hi", @"57", @"i am ready",nil];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSError *error;
NSData *postdata = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:&error];

NSString *serverURL = @"http://xyzabc.com/web/IPAD/v1.0/webservices.php?type=Suggestions";
// Create the POST request to the server.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:serverURL]];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [postdata length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postdata];
NSURLResponse *response;
NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
NSString *theReply = [[NSString alloc] initWithBytes:[POSTReply bytes] length:[POSTReply length] encoding: NSASCIIStringEncoding];
NSLog(@"Reply: %@", theReply);

我收到错误消息回复:{“status”:0,“error”:“Missing Subject(OR)message”}。请更正我的代码...

提前致谢

0 个答案:

没有答案