2012-02-24 80 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」公共職能進行訪問。