ASIHttp请求 POST

发布于 2024-12-12 19:44:37 字数 339 浏览 0 评论 0原文

我正在开发一个 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 技术交流群。

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

发布评论

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

评论(2

别忘他 2024-12-19 19:44:38

如果您的目标是 iOS 4+,则可以使用块,这使得异步代码变得更好一些。您不必与代表打交道。

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];

[request setCompletionBlock:^{
    NSLog(@"%@", request.responseString);
}];

[request setFailedBlock:^{

    NSLog(@"%@", request.error);
}];

[request startAsynchronous];    

有关块的更多信息
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.

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];

[request setCompletionBlock:^{
    NSLog(@"%@", request.responseString);
}];

[request setFailedBlock:^{

    NSLog(@"%@", request.error);
}];

[request startAsynchronous];    

More about blocks
http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

樱花坊 2024-12-19 19:44:38

您可以使用它来发布同步响应,响应将位于 var“response”中。

- (IBAction)grabURL:(id)sender
{
  NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
    NSString *response = [request responseString];
  }
}

要发出异步请求,请执行以下操作:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

基本上,当有数据时,将调用完成的请求,具体取决于您可以获取 NSData 或 NSString 的格式,如示例中所示。

您可以像您自己所说的那样输入参数:

[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];

我使用了普通类型的请求,您可以将 ASIHTTPRequest 类替换为 ASIFormDataRequest 。

This you use for posting a synchronous response, the response will be in the var "response".

- (IBAction)grabURL:(id)sender
{
  NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
  ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
  [request startSynchronous];
  NSError *error = [request error];
  if (!error) {
    NSString *response = [request responseString];
  }
}

To make a async request, do the following:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

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:

[request addPostValue:@"Ben" forKey:@"names"];
[request addPostValue:@"George" forKey:@"names"];

I have used a normal type of requesting, you could replace the ASIHTTPRequest class with ASIFormDataRequest.

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