【问题标题】:HTTP POST JSON MethodHTTP POST JSON 方法
【发布时间】:2011-08-31 02:14:48
【问题描述】:

您好,我需要帮助来建立与 Web 服务的 HTTP POST 连接。我完全不知道该怎么做。

我只在制作 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'.

是因为我没有传递一些值(lastmodified 和passengerId)吗?我不知道他们为什么需要这两个参数,因为这个请求只是创建一个新帐户。

【问题讨论】:

  • ASIHTTPRequest。使用appendPostData:appendPostDataFromFile: 方法添加JSON dict。

标签: iphone json http post


【解决方案1】:

您可以按照@darvids0n 的建议使用ASIHTTPRequest,也可以使用标准的iOS NSMutableURLRequest 方法,如下所示:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:myURL]; // Assumes you have created an NSURL * in "myURL"
[request setHTTPMethod:@"POST"];
[request setHTTPBody:myJSONData]; // Assumes you have created an NSData * for your JSON
[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

无论您选择如何执行此操作,您都需要将您的 Objective-C 对象转换为 JSON 字符串,然后将其转换为 NSData。下面是一个示例,说明如何使用 SBJSON 框架同时执行这两个步骤。

SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSData *myJSONData = [writer dataWithObject:myDataDictionary];

上面将字典转换为 JSON 字符串,然后使用字符串的 UTF8 编码将该字符串转换为 NSData。如果你想要不同的编码,你将不得不打破使用[writer stringWithObject:myDataDictionary] 并自己编码。

【讨论】:

  • 所以我需要创建一个包含 4 个对象(lastmodified、passengerID、...)的 NSDictionary 并将它们转换为 NSData,最后使用 json 解析器将其转换为 JSON 数据?
  • 通常您会将对象放入字典中,然后使用诸如 SBJSON (stig.github.com/json-framework) 之类的框架将该字典转换为 JSON 字符串。 SBJSONStreamWriter 类可以将 NSDictionary 转换为字节流。您可以将自己设置为代表来接收这些字节并将它们附加到 NSData 对象中。获得 NSData 后,您可以将其添加到 HTTP POST 请求中。
  • 我遇到了一些示例,他们将 HTTPBody 设置为 JSONString。嗯?
  • setHTTPBody 需要一个 NSData 对象作为输入参数。但是,您将数据值转换为 JSON,然后需要将该 JSON 转换为 NSData。如果可以的话,从 JSON 字符串转换为 NSData 非常容易。 SBJSON 只是解析/生成 JSON 的一种框架。不过,这不是唯一的方法。
  • 你能看看我编辑的答案,并建议调用将 JSONString 转换为 JSONData 的方法是什么?如果某些陈述似乎不正确,请告知。
猜你喜欢
  • 1970-01-01
  • 2017-03-16
  • 2011-10-21
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多