gpt4 book ai didi

ios - iPhone:如何使用 Xcode 向 Web 服务发送 HTTP 请求

转载 作者:可可西里 更新时间:2023-11-01 03:04:16 24 4
gpt4 key购买 nike

如何使用 Objective-C 向 Web 服务发送 HTTP 请求?我需要从我的 MySQL 数据库中获取一些数据,因此我需要发送请求以便获取数据。

最佳答案

编辑,因为这是一个热门问题,时间还在继续。与此同时,Apple 引入了 NSJSONSerialization。查看文档:https://developer.apple.com/documentation/foundation/jsonserialization仅当您需要为 iOS 5.0 之前的版本创建代码时,您才可能需要使用下面提到的 json-framwork。

除此之外,以下原答案仍然有效:

假设您使用 JSON 交换数据,您可能会对阅读本文感兴趣。 http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html

但是,该文章的第一部分回答了关于如何从网络服务器/服务接收数据的更一般的问题:

- (NSString *)stringWithUrl:(NSURL *)url
{
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// Fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;

// Make synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];

// Construct a String around the Data from the response
return [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
}

这是一个简单的 HTTP 请求示例,对于您接下来的步骤肯定足够了。此方法被赋予一个 URL,它返回服务器在 NSString 中发送的数据。

一旦它正常工作,您将希望避免您的应用程序在请求“播出”时卡住。您可以通过异步请求来解决这个问题。 Google 或 stackoverflow 上的搜索将为您提供相关示例和教程的链接。但我建议先从这里开始,尽管这可能不是最终的解决方案。

关于ios - iPhone:如何使用 Xcode 向 Web 服务发送 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7262484/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com