如何使用NSURLRequest发送Http请求中的json数据

80

我刚接触Objective-C,最近开始花费大量时间在请求/响应方面。我有一个可以调用URL(通过HTTP GET)并解析返回的JSON的工作示例。

以下是这个工作示例:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
  NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [connection release];
  //do something with the json that comes back ... (the fun part)
}

- (void)viewDidLoad
{
  [self searchForStuff:@"iPhone"];
}

-(void)searchForStuff:(NSString *)text
{
  responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];
}

我的第一个问题是 - 这种方法能够扩展吗?或者说它不是异步的(意味着当应用程序等待响应时,我会阻塞UI线程)

我的第二个问题是 - 我该如何修改这个请求部分以执行POST而不是GET?只需要修改HttpMethod吗?

[request setHTTPMethod:@"POST"];

最后,我该如何将一组JSON数据作为简单字符串添加到此帖子中(例如)?

{
    "magic":{
               "real":true
            },
    "options":{
               "happy":true,
                "joy":true,
                "joy2":true
              },
    "key":"123"
}

提前致谢


1
这是一个教程:http://mobileorchard.com/tutorial-json-over-http-on-the-iphone/ - Josh
8个回答

104

以下是我的操作(请注意,发送到我的服务器的JSON需要是一个字典,其中 key = question 的值为另一个字典,即 {:question => { dictionary } }):

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"],
  [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],     nil];
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];
          
NSString *jsonRequest = [jsonDict JSONRepresentation];

NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
             cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSData *requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (connection) {
 receivedData = [[NSMutableData data] retain];
}

接收到的数据随后由以下方式处理:

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [jsonString JSONValue];
NSDictionary *question = [jsonDict objectForKey:@"question"];

这不是完全清晰的,需要反复阅读一下,但在这里应该有足够的信息可以让你开始。从我的观察来看,这是异步的。我的用户界面在进行这些调用时没有被锁定。


1
抱歉。是的,“dict”只是一个简单的字典,我从iOS用户文档中加载它。 - Mike G
19
这是使用 NSDictionary 实例方法 JSONRepresentation。我建议您使用 NSJSONSerialization 类方法 dataWithJSONObject,而不是 json-framework - Rob
1
@MikeG 修复了代码示例中一个长期存在但迄今未被注意到的错误。很抱歉修改了你的帖子 ;) - CouchDeveloper
我们能否使用GET代替POST来实现相同的功能? - Satyam
@Rob,我的编辑被以3比2的投票拒绝了,所以我把我的编辑变成了一个答案,链接在这里:https://dev59.com/DW855IYBdhLWcg3wPBz6#33505940。 - Steve Moser
显示剩余5条评论

7

我曾经为此苦苦挣扎,要在服务器上运行PHP代码。以下代码将会发送一个json请求并获取服务器返回的json响应。

NSURL *url = [NSURL URLWithString:@"http://example.co/index.php"];
NSMutableURLRequest *rq = [NSMutableURLRequest requestWithURL:url];
[rq setHTTPMethod:@"POST"];
NSString *post = [NSString stringWithFormat:@"command1=c1&command2=c2"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding];
[rq setHTTPBody:postData];
[rq setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:rq queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     if ([data length] > 0 && error == nil){
         NSError *parseError = nil;
         NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
         NSLog(@"Server Response (we want to see a 200 return code) %@",response);
         NSLog(@"dictionary %@",dictionary);
     }
     else if ([data length] == 0 && error == nil){
         NSLog(@"no data returned");
         //no data, but tried
     }
     else if (error != nil)
     {
         NSLog(@"there was a download error");
         //couldn't download

     }
 }];

1
内容类型为 "application/x-www-form-urlencoded" 就解决了问题。谢谢。 - SamChen
好的答案。在我的情况下,我使用了“application / json”。 - Gajendra K Chauhan

6
我建议使用ASIHTTPRequest
ASIHTTPRequest是一个易于使用的封装器,它包裹了CFNetwork API,并使得与Web服务器通信中一些更繁琐的方面变得更加容易。它用Objective-C编写,在Mac OS X和iPhone应用程序中都可以使用。
它适合执行基本的HTTP请求并与REST服务进行交互(GET / POST / PUT / DELETE)。其中包括的ASIFormDataRequest子类使得使用multipart/form-data提交POST数据和文件变得简单。
请注意,原作者已停止此项目。请参阅以下帖子了解原因和替代方案:http://allseeing-i.com/%5Brequest_release%5D
个人而言,我是AFNetworking的忠实粉丝。

3

大多数人现在已经知道了,但我还是要发布这篇文章,以防一些人仍然在iOS6+中遇到JSON的问题。

iOS6及更高版本中,我们有NSJSONSerialization类,它快速且不依赖于包含“外部”库。

NSDictionary *result = [NSJSONSerialization JSONObjectWithData:[resultStr dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; 

这是iOS6及更高版本可以高效解析JSON的方法。如果您在ARC环境下工作,则使用SBJson也需要进行预先ARC实现,并带来相应问题。
希望这能帮到您!

2

由于我对Mike G的回答进行现代化代码编辑被以3比2的投票拒绝,我感到非常遗憾。

这次编辑旨在回复文章作者,作为编辑并没有任何意义。它应该写成评论或答案。

我将我的编辑作为一个独立的答案重新发布在这里。根据Rob的评论(15个赞同),这个编辑使用NSJSONSerialization移除了JSONRepresentation依赖。

    NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"],
      [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],     nil];
    NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];
    NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

    NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];

    NSLog(@"jsonRequest is %@", jsonRequest);

    NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                 cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


    NSData *requestData = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil]; //TODO handle error

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody: requestData];

    NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    if (connection) {
     receivedData = [[NSMutableData data] retain];
    }

接收到的数据随后由以下方式处理:
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    NSDictionary *question = [jsonDict objectForKey:@"question"];

2
这是一篇很棒的文章,使用了Restkit
它解释了如何将嵌套数据序列化为JSON,并将数据附加到HTTP POST请求中。

0
你可以尝试使用这段代码来发送JSON字符串。
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ARRAY_CONTAIN_JSON_STRING options:NSJSONWritin*emphasized text*gPrettyPrinted error:NULL];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSString *WS_test = [NSString stringWithFormat:@"www.test.com?xyz.php&param=%@",jsonString];

0
这是一个更新的示例,使用NSURLConnection + sendAsynchronousRequest: (10.7+, iOS 5+),与已接受答案相同的“Post”请求保持不变,为了清晰起见在此省略。
NSURL *apiURL = [NSURL URLWithString:
    [NSString stringWithFormat:@"http://www.myserver.com/api/api.php?request=%@", @"someRequest"]];
NSURLRequest *request = [NSURLRequest requestWithURL:apiURL]; // this is using GET, for POST examples see the other answers here on this page
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
     if(data.length) {
         NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
         if(responseString && responseString.length) {
             NSLog(@"%@", responseString);
         }
     }
}];

问题是关于POST的。 - ahmad
2
不,问题的第一部分是关于异步性的,这里没有任何回答能够解决这个问题。感谢你的投票。 - auco

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接