ASIHTTPRequest 上的多个请求 [英] Multiple Requests on ASIHTTPRequest

查看:23
本文介绍了ASIHTTPRequest 上的多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从三个不同的 URL 下载三组不同的数据.我决定使用 ASIHTTPRequest.其中两个 URL 是我需要解析的 JSON 提要,其中一个是我需要在本地存储的在线 .txt 文件.

现在 ASIHTTPRequest 网站上的异步请求示例显示如下:

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

要传递多个 URL,我可以对三个不同的 URL 调用请求".但我不确定如何在 requestFinished 方法中处理它们.文档显示为:

- (void)requestFinished:(ASIHTTPRequest *)request {//获取文本数据时使用NSString *responseString = [请求响应字符串];//获取二进制数据时使用NSData *responseData = [请求响应数据];}

这个方法如何区分不同的请求,以便我可以不同地处理它?<​​/p>

谢谢,

解决方案

你可以通过

区分不同的请求
  • 设置请求的userInfo字典
  • 将 didFinishSelector(和 didFailSelector 等)设置为不同的方法
  • 使用不同的类作为委托
  • 使用块
  • 使用请求的标签属性
  • 子类 ASIHTTPRequest 并覆盖覆盖 requestFinished: 和 failWithError:(仅推荐用于复杂情况)

I need to download three different sets of data from three different URLs. I decided to use ASIHTTPRequest. Two of the URLs are JSON feeds which I need to parse and one of them is a .txt file online that I need to store locally.

Now the example that is on ASIHTTPRequest's website for an asynchronous request shows the following:

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

To pass multiple URLs, I can call "request" on three different URLs. But I am not sure how I would handle them in the requestFinished method. The documentation shows it as:

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

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

How would this method differentiate between different requests, so that I can handle it differently?

Thank you,

解决方案

You can differentiate between different requests by

  • setting the userInfo dictionary of the request
  • setting the didFinishSelector (and didFailSelector etc.) to different methods
  • using different classes as delegate
  • using blocks
  • using the request's tag property
  • subclass ASIHTTPRequest and override override requestFinished: and failWithError: (only recommended for complex situations)

这篇关于ASIHTTPRequest 上的多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆