ASIHttp请求 POST
我正在开发一个 iPhone 应用程序,我的要求是向 Web 服务发送一个简单的帖子,并且 Web 服务返回一个 json 响应。
他们为帖子提供的示例代码是这样的,但是我如何获得回复?我看到有用于执行 get 的异步代码,但是如何为 post 执行异步代码?
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
I am working on an iphone app and my requirements are to make a simple post to a web service and the web service returns a json response.
The sample code they give for a post is this, but how do I get a response? I see there is asynchronous code for doing a get, but how do I do it async for a post?
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是 iOS 4+,则可以使用块,这使得异步代码变得更好一些。您不必与代表打交道。
有关块的更多信息
http://developer.apple.com/库/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html
If you're targeting iOS 4+ you can use blocks, which makes asynchronous code a little bit nicer. You won't have to deal with delegates.
More about blocks
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html
您可以使用它来发布同步响应,响应将位于 var“response”中。
要发出异步请求,请执行以下操作:
基本上,当有数据时,将调用完成的请求,具体取决于您可以获取 NSData 或 NSString 的格式,如示例中所示。
您可以像您自己所说的那样输入参数:
我使用了普通类型的请求,您可以将 ASIHTTPRequest 类替换为 ASIFormDataRequest 。
This you use for posting a synchronous response, the response will be in the var "response".
To make a async request, do the following:
Basically the request finished will be called when there is data, depending on the format you can get either a NSData or a NSString like in the example.
Your params you can put in like you said yourself:
I have used a normal type of requesting, you could replace the ASIHTTPRequest class with ASIFormDataRequest.