向服务器发送 JSON 请求?

发布于 2024-12-17 23:53:48 字数 1340 浏览 3 评论 0原文

我有以下 JSON 用于在服务器上发送请求

{
    "name":"Home",
    "latitude":"45.5037078163837",
    "longitude":"-122.622699737549",
    "radius":"240"
}

,而请求的 URL 是

  URL: https://api.geoloqi.com/1/place/create 

我正在发出这样的请求,

    NSString *urlString= @"https://api.geoloqi.com/1/place/create ";
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";

jsonRequest = [self JSONString:jsonRequest];

NSData* requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [requestData length]];

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval:30.0];
[url release];
[requestData release];
[requestDataLengthString release];

NSURLConnection *m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];

这个请求有什么问题吗?

I have following JSON for sending request on server

{
    "name":"Home",
    "latitude":"45.5037078163837",
    "longitude":"-122.622699737549",
    "radius":"240"
}

and URL of for request is

  URL: https://api.geoloqi.com/1/place/create 

I am making request like this,

    NSString *urlString= @"https://api.geoloqi.com/1/place/create ";
NSURL* url = [[NSURL alloc] initWithString:urlString];
NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";

jsonRequest = [self JSONString:jsonRequest];

NSData* requestData = [jsonRequest dataUsingEncoding:NSUTF8StringEncoding];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [requestData length]];

NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:requestData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setTimeoutInterval:30.0];
[url release];
[requestData release];
[requestDataLengthString release];

NSURLConnection *m_URLConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];

What is wrong with this request ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趁微风不噪 2024-12-24 23:53:49

您的 JSON 字符串在经度和半径值后似乎缺少 2 个引号:

Change

NSString *jsonRequest = @"{\"名称\":\"转向\",\"纬度\":\"45.5037078163837\",\"经度\":\"-122.622699737549\,\"半径\":\"500\ } ";

到此

NSString *jsonRequest = @"{\"名称\":\"转向\",\"纬度\":\"45.5037078163837\",\"经度\":\"-122.622699737549\",\"半径\":\"500\ “}”;

Your JSON string seems to be missing 2 quotes after the longitude and radius values:

Change

NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\,\"radius\":\"500\ }";

To this

NSString *jsonRequest = @"{\"name\":\"veer\",\"latitude\":\"45.5037078163837\",\"longitude\":\"-122.622699737549\",\"radius\":\"500\" }";

十秒萌定你 2024-12-24 23:53:49

从你的问题中尚不清楚你的问题是什么。

不管怎样,你也在做:

[m_URLConnection start];

创建连接后吗?

创建连接

或者,您可以使用[– initWithRequest:delegate:startImmediately:][1]

,它允许您指定立即开始加载数据。

还可以考虑使用 [+sendAsynchronousRequest:queue:completionHandler:][2] 作为连接的便捷构造函数。

It is not clear from your question what is your issue with that.

Anyway, are you doing also:

[m_URLConnection start];

after creating the connection?

Alternatively, you could create the connection using

[– initWithRequest:delegate:startImmediately:][1]

which allows you to specify that the loading of data shall start immediately.

Also consider using [+sendAsynchronousRequest:queue:completionHandler:][2] as a convenience constructor for your connection.

屌丝范 2024-12-24 23:53:49

您尝试访问的 Web 服务 URL 似乎正在使用 SSL。您可能需要将访问令牌放入请求标头中,以便从 Web 服务获得正确的响应。

认证请查看链接:
https://developers.geoloqi.com/api/authentication

The Webservice URL you are trying to hit is seem to be using SSL. You may need to put access token in request header in order to get the proper response from web service.

Please see the link for authentication:
https://developers.geoloqi.com/api/authentication

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文