【问题标题】:Get postValue from finished ASIHTTPRequest从完成的 ASIHTTPRequest 中获取 postValue
【发布时间】:2012-06-22 09:36:32
【问题描述】:

我想知道如何从完成的 ASIHTTPRequest 中检索 post 值。

当我执行请求时,我会这样做:

[request setPostValue:uniqueID forKey:@"bookUniqueId"];
NSFileManager *fileManager = [[NSFileManager alloc]init];
if (![fileManager fileExistsAtPath:tempDir]) {
    [fileManager createDirectoryAtPath:tempDir withIntermediateDirectories:YES attributes:nil error:nil];
}
[fileManager release];

tempDir = [[tempDir stringByAppendingPathComponent:uniqueID]stringByAppendingPathExtension:@"zip"];
[request setDownloadDestinationPath:tempDir];
[request setDelegate:self];
[request startAsynchronous];

因为我想下载 zip 文件,名称与我的书的 ID 相同。

- (void)requestFinished:(ASIHTTPRequest *)request 方法中,我需要该ID 来解压缩文件。我无法将 ID 保存在 ivar 中,因为我想支持多个异步下载。

我该怎么做?有没有类似不存在的方法[request postValueForKey: key]的东西?

【问题讨论】:

  • 请注意 ASIHTTPRequest 库已被弃用。我不会推荐它用于新项目。

标签: iphone objective-c ios ipad asihttprequest


【解决方案1】:

每当您创建 ASIFormDataRequest 时,只需设置其 userinfo 并像这样获取它。

用于设置用户信息使用此。

[request setUserInfo:[NSDictionary dictionaryWithObject:@"YOUR_VALUE" forKey:@"YOUR_KEY"]];

要获取用户信息,请使用此。

-(void)requestFinished:(ASIHTTPRequest *)request
{
     NSDictionary *dict = [request userInfo];
     NSString *str = [dict objectForKey:@"YOUR_KEY"];
}

【讨论】:

    【解决方案2】:

    你可以使用ASIHTTPRequestuserInfo属性

    喜欢下面的

    request.userInfo = [[NSMutableDictionary alloc] init];
    [request.userInfo setValue:yourID forKey:@"your id"];    
    

    然后在requestFinished

    - (void)requestFinished:(ASIHTTPRequest *)request
        yourid = [request userInfo] valueForKey:@"your id"];
    

    【讨论】:

    • 非常感谢,我会接受马文的回答,只是因为他是第一个回答的......
    猜你喜欢
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多