iphone - ASIHTTPRequest 随每个请求发送数据

标签 iphone objective-c ios ipad asihttprequest

我有一个包含 JSON 提要的 URL 的数组。我正在使用 ASIHTTPRequest 下载提要并进行处理。每个提要包含多个 JSON 条目或对象。 request下载数据并仅选择一个对象并将其存储。

供稿 URL 如下所示:http:www。 * .com/id.json,其中 id是一些字符串。下载数据并选择对象后,我想将 id 作为映射到下载对象值的键存储在字典中。

如何通过请求传递该字符串?例如:

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
request.tag = 3;
[request setDelegate:self];
[request startAsynchronous];

现在在 requestFinished ,我可以按如下方式识别该请求:if (request.tag == 3 .连同 tag 3,我想发送ID。所以我可以在 if (request.tag == 3) 中用它做点什么.是否有一些属性可以让我在请求中传递字符串或任何数据?

最佳答案

您可以在 userInfo 中传递您自己的数据字典。属性,如 tag属性,可以在收到响应后在请求上回读。

NSString* jsonId = @"1234";
request.userInfo = [NSDictionary dictionaryWithObject:jsonId forKey:@"id"];

documentation .

If you need to handle success and failure on many different types of request, you have several options:

  1. If your requests are all of the same broad type, but you want to distinguish between them, you can set the userInfo NSDictionary property of each request with your own custom data that you can read in your finished / failed delegate methods. For simpler cases, you can set the request’s tag property instead. Both of these properties are for your own use, and are not sent to the server.

  2. If you need to handle success and failure in a completely different way for each request, set a different setDidFinishSelector / setDidFailSelector for each request



如果您想像网页发布表单一样发布数据,可以使用 ASIFormDataRequest 子类。它使发送带有您单独添加的字符串的 POST 请求变得非常容易:
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"Ben" forKey:@"first_name"];
[request setPostValue:@"Copsey" forKey:@"last_name"];

documentation .

关于iphone - ASIHTTPRequest 随每个请求发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7134943/

相关文章:

iphone - 如果 iPhone 上可用,如何构建仅支持 Game Center 的游戏?

ios - 手动设置 UIButton 状态

iphone - 如何检查数组中的值是否不为 NULL?

ios - 无法找到目标 'IQKeyboardManagerSwift' 的模块 'armv7-apple-ios' ;

iOS 使 UIScrollView 分页仅对一个方向启用

ios - 在范围内找不到类型 'SKAdImpression'

android - 如何在新层中使用phonegap打开外部链接?

ios - 如何避免iOS项目重复符号链接(symbolic link)错误?

objective-c - 将字符串添加到数组

ios - 在 Objective-C 中使用 NSLog 时取消引用指针