ios iPhone:如何使用 Xcode 向 Web 服务发送 HTTP 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7262484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iPhone: How to send a HTTP request to a web service using Xcode
提问by Illep
How to send a HTTP request to a web service using Objective-C ? I need to fetch some data from my MySQL DB, so I need to send the request so I could fetch the data.
如何使用 Objective-C 向 Web 服务发送 HTTP 请求?我需要从我的 MySQL 数据库中获取一些数据,所以我需要发送请求以便我可以获取数据。
回答by Hermann Klecker
Edit, because this is a popular question and time keeps going on. In the meantime Apple introduced NSJSONSerialization. Have a look a the docs: https://developer.apple.com/documentation/foundation/jsonserializationOnly if you need to create code for iOS earlier than 5.0 you may want to use the json-framwork as mentioned below.
编辑,因为这是一个流行的问题,时间一直在继续。与此同时,Apple 引入了 NSJSONSerialization。查看文档:https: //developer.apple.com/documentation/foundation/jsonserialization只有当您需要为 5.0 之前的 iOS 创建代码时,您才可能需要使用下面提到的 json-framework。
Besides that, the following original answer is still valid:
除此之外,以下原始答案仍然有效:
Assuming that you exchange your data in JSON, you may be interested in reading this. http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html
假设您以 JSON 格式交换数据,您可能有兴趣阅读本文。 http://iosdevelopertips.com/cocoa/json-framework-for-iphone-part-2.html
However, the first part of that article answers your more general question on how to receive data from a web server/service:
但是,该文章的第一部分回答了您关于如何从 Web 服务器/服务接收数据的更普遍的问题:
- (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];
}
This is a simple example of a http request and surely good enough for your next steps. This method is given a URL and it returns the data sent by the server in an NSString.
这是一个简单的 http 请求示例,对于您的后续步骤来说肯定足够了。该方法被赋予一个 URL,它以 NSString 形式返回服务器发送的数据。
Once that is working properly you will like to avoid that your app freezes while the request is "on the air". You can fix that by asynchronous requests. Google or the search on stackoverflow will provide you with links to examples and tutorials about that. But I suggest to go from here first, although this may not be the final solution.
一旦它正常工作,您将希望避免在请求“播出”时您的应用程序冻结。您可以通过异步请求来解决这个问题。Google 或 stackoverflow 上的搜索将为您提供有关示例和教程的链接。但我建议先从这里开始,尽管这可能不是最终的解决方案。
回答by picciano
There are many ways, but perhaps the easiest is to use an existing framework, ASIHTTPRequest.
有很多方法,但也许最简单的方法是使用现有框架 ASIHTTPRequest。
回答by Eugene Nguyen
Following is a lot of very famous library you can use for request
以下是许多非常有名的图书馆,您可以用于请求
ASIHTTPRequest
ASIHTTP请求
AFNetwork
AF网络
MFNetwork
MF网络
You can search it in google, download in github or, yes, of course, why don't use Cocoapods. that's the best thing when i compare objective-c with other language.
你可以在google上搜索,在github上下载,或者,是的,当然,为什么不使用Cocoapods。当我将 Objective-c 与其他语言进行比较时,这是最好的事情。
Good luck
祝你好运
回答by Tim Dean
ASIHTTPRequest is a great and simple framework, so I second the recommendations from other posts. To be complete though, you should also know that you can do this natively in Objective-C without another framework. Look at the Apple documentation for the NSURLConnection and NSURLConnectionRequest classes for background.
ASIHTTPRequest 是一个伟大而简单的框架,所以我附和其他帖子的建议。尽管如此,您还应该知道,您可以在没有其他框架的情况下在 Objective-C 中本地完成此操作。查看 Apple 文档,了解 NSURLConnection 和 NSURLConnectionRequest 类的背景信息。