OGeek|极客世界-中国程序员成长平台

标题: ios - 将 JSON 数据发送到 URL [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 21:14
标题: ios - 将 JSON 数据发送到 URL

我有一个特定的 URL “http://dev.mycompany.com”,我需要向它发送一些数据(JSON 格式)并获得响应。

我无法通过 SO 上的大量文档和相关问题找到出路。我已经设法使用 NSURLConnection 获取数据(不发送任何数据)并且效果很好,到目前为止我的代码与 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html 上的代码几乎相同所以我没有看到发布我拥有的代码的任何意义。

我无法将一些字符串附加到我的 URL,因为我需要发送的数据是 JSON 数据。如果我听起来像个菜鸟,我很抱歉,但我在 Obj-C 和服务器通信方面的经验很少。



Best Answer-推荐答案


您应该将它们作为参数发送到帖子 NSUrlRequest

如下:

NSURL *aUrl = [NSURL URLWithString"http://dev.mycompany.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:aUrl
                                         cachePolicy:NSURLRequestUseProtocolCachePolicy
                                     timeoutInterval:60.0];
[request setHTTPMethod"OST"];
NSString *postString = @"yourVarialbes=yourvalues";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection= [[NSURLConnection alloc] initWithRequest:request 
                                                             delegate:self];
[connection start];

用于发送 JSON 请阅读 How to send json data in the Http request using NSURLRequest

关于ios - 将 JSON 数据发送到 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10864497/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4