您好,我需要帮助进行HTTP POST连接到Web服务。我对如何做到完全无能为力。HTTP POST JSON方法
我只在制作NSURLConnection方面经验丰富,因此我将所需的输入参数作为URL的一部分传递。现在看起来不同,因为我只提供了一个网站和请求JSON正文。
{
"lastmodified":"String content",
"passengerId":9223372036854775807,
"password":"String content",
"userId":"String content"
}
任何人都可以对此有所了解吗?说这个网站是www.myURL/operations/AddItem.com
编辑 - 我在这里做错了什么?
NSError *theError = nil;
NSArray *keys = [NSArray arrayWithObjects:@"userId", @"password", nil];
NSArray *objects = [NSArray arrayWithObjects:userNameTextField.text, passwordTextField.text, nil];
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSString *myJSONString =[jsonDictionary JSONRepresentation];
NSData *myJSONData =[myJSONString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"myJSONString :%@", myJSONString);
NSLog(@"myJSONData :%@", myJSONData);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://153.20.32.74/11AprP306/passenger/item"]];
[request setHTTPBody:myJSONData];
[request setHTTPMethod:@"POST"];
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&theError];
NSLog(@"response : %@", theResponse);
NSLog(@"error : %@", theError);
NSLog(@"data : %@", data);
NSMutableString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string: %@", string);
NSDictionary *jsonDictionaryResponse = [string JSONValue];
NSLog(@"dic: %@", jsonDictionaryResponse);
[string release];
[theResponse release];
我读string: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1 transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Request Error</title> <style>BODY {..... 'The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'.
难道是因为我已经过短一些值(上次更改时间& passengerId)?我不知道为什么他们需要这两个参数,因为这个请求只是创建一个新的帐户。
['ASIHTTPRequest'](http://allseeing-i.com/ASIHTTPRequest)。使用'appendPostData:'或'appendPostDataFromFile:'方法来添加JSON字典。 – darvids0n