2012-02-24 82 views
2

如何访问使用requestFailed/requestFinished函数请求发送的(POST)数据。访问请求发送数据,请求内部失败函数ASIHttpRequest

- (void) abc { 
    NSString *postString = @"john"; 
    NSURL *url = [NSURL URLWithString:@"http://abc.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setPostValue:postString forKey:@"name"]; 
    [request setDelegate:self]; 
    [request startAsynchronus]; 
} 
- (void) requestFinished:(ASIHTTPRequest *)request 
{ 
    // Question is whether the request holds the sent post values. 
    // If it holds. how can we access them. 
    // i tried using [request valueForKey:@"name"]; 
    // but it won't work. 
} 

回答

0

Handling success and failure for multiple requests in delegate methods

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

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.

If you need to handle success and failure in a completely different way for each request, set a different setDidFinishSelector/setDidFailSelector for each request For more complex situations, or where you want to parse the response in the background, create a minimal subclass of ASIHTTPRequest for each type of request, and override requestFinished: and failWithError:.

这为我提供了一个很好的解决方案来处理不同的请求。

+0

是啊从ASIHTTPREQUEST文档中得到了..无论如何感谢信息。 – divein 2012-04-22 01:54:43

0

你可以试试这个 -

- (void)requestFinished:(ASIHTTPRequest *)request { 
    NSLog(@"Response %d ==> %@", request.responseStatusCode, [request responseString]); 
} 

您也可以处理,如果你选择其他的方法,如:

- (void)requestStarted:(ASIHTTPRequest *)request; 
- (void)requestFailed:(ASIHTTPRequest *)request; 

该文档位于http://allseeing-i.com/ASIHTTPRequest/和是太棒了。

+0

是的。我可以访问responseString ..问题是我不能makeout我哪个请求处理响应..请求参数区分b/w请求..我需要访问请求参数(dat与请求一起发送).. – divein 2012-02-25 05:20:24

0

你可以投你的请求转换成ASIFormDataRequest:

if ([request isKindOfClass:[ASIFormDataRequest class]]) { 
    ASIFormDataRequest *requestWithPostDatas = (ASIFormDataRequest *)request; 
    NSArray *myPostData = [requestWithPostDatas getPostData]; 
} 

你也将不得不作出“POSTDATA”在ASIFormDataRequest一个“getPostData”公共职能进行访问。