2010-12-02 44 views
0

當我使用選擇器爲成功和失敗註冊回調時,當使用asi-http-request時,我總是收到一個非常奇怪的錯誤。asi-http-request異步選擇器錯誤

以下方法用於:

// Sets up a request and takes care of the stuff that needs to be for each 
// and every request. 
- (void) setup:(NSString *) fragment successCallback:(SEL) success_callback failureCallback:(SEL) failure_callback { 

    // Convert the string to a url 
    NSString *url = [[NSString alloc] initWithFormat:@"%@%@", 
    self.server.serverUrl, 
    fragment 
    ]; 
    NSURL *full_url = [NSURL URLWithString: url]; 

    // Time to setup the request 
    self.curRequest = [ASIFormDataRequest requestWithURL:full_url]; 
[self.curRequest setDelegate:self]; 
[self.curRequest setDidFailSelector:success_callback]; 
[self.curRequest setDidFinishSelector:failure_callback]; 

// If we're using iphone os version 4.0 
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 
[self.curRequest setShouldContinueWhenAppEntersBackground:YES]; 
#endif 

// Clean up time! 
[url release]; 
[full_url release]; 

}

寄存器方法

- (void) register_user:(NSString *) email password:(NSString *) password confirmPassword:(NSString *) confirm_password successCallback:(SEL) success_callback failureCallback:(SEL) failure_callback { 

[自設置:@ 「/用戶/註冊」 successCallback:success_callback failureCallback: failure_callback ];

//設置方法的輸入 [self.curRequest setPostValue:email forKey:@「email」]; [self.curRequest setPostValue:password forKey:@「password」]; [self.curRequest setPostValue:confirm_password forKey:@「confirm_password」]; NSLog(@「我們到了這裏喲:)〜%@%@%@」,email,password,confirm_password);

[self.curRequest startAsynchronous]; }

成功和失敗回調

- (void) registerSuccess: (ASIHTTPRequest *) request { 
    NSString *response = [request responseString]; 
    NSLog(@"Response string: %s", response); 
} 

// Called when the http request fails. That means it could not 
// reach the server for an unknown reason. 
- (void) registerFailure: (ASIHTTPRequest *) request { 
    NSError *error = [request error]; 
    NSLog(@"Register error: %s", [error localizedFailureReason]); 
} 

我喜歡調用REGISTER方法,以便:

[self.appdelegate.userModel register_user:self.emailAddress.text 密碼:self.password.text confirmPassword:self.confirmPassword.text successCallback:@selector(registerSuccess :) failureCallback:@selector(registerFailure :) ];

從GDB調用堆棧和錯誤:

2010-12-02 12:33:56.889 ProjectPrototype[2201:6003] -[__NSCFType absoluteURL]: unrecognized selector sent to instance 0x6457c60 
2010-12-02 12:33:56.891 ProjectPrototype[2201:6003] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType absoluteURL]: unrecognized selector sent to instance 0x6457c60' 
*** Call stack at first throw: 
(
    0 CoreFoundation      0x0292eb99 __exceptionPreprocess + 185 
    1 libobjc.A.dylib      0x02a7e40e objc_exception_throw + 47 
    2 CoreFoundation      0x029306ab -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 
    3 CoreFoundation      0x028a02b6 ___forwarding___ + 966 
    4 CoreFoundation      0x0289fe72 _CF_forwarding_prep_0 + 50 
    5 CoreFoundation      0x02843b04 CFURLCopyAbsoluteURL + 100 
    6 CFNetwork       0x023fe5e8 _ZN11HTTPMessage10initializeEPK10__CFStringPK7__CFURLS2_ + 52 
    7 CFNetwork       0x023fe50c CFHTTPMessageCreateRequest + 80 
    8 ProjectPrototype     0x00011360 -[ASIHTTPRequest main] + 852 
    9 Foundation       0x000c83ca __NSThreadPerformPerform + 251 
    10 CoreFoundation      0x0290ffaf __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15 
    11 CoreFoundation      0x0286e39b __CFRunLoopDoSources0 + 571 
    12 CoreFoundation      0x0286d896 __CFRunLoopRun + 470 
    13 CoreFoundation      0x0286d350 CFRunLoopRunSpecific + 208 
    14 CoreFoundation      0x02870614 CFRunLoopRun + 84 
    15 ProjectPrototype     0x00023b55 +[ASIHTTPRequest runRequests] + 173 
    16 Foundation       0x000b376c -[NSThread main] + 81 
    17 Foundation       0x000b36f8 __NSThread__main__ + 1387 
    18 libSystem.B.dylib     0x9689881d _pthread_start + 345 
    19 libSystem.B.dylib     0x968986a2 thread_start + 34 
) 
terminate called after throwing an instance of 'NSException' 

我明白任何及所有的幫助,非常感謝:)〜

回答

4

Dudester你有兩個嚴重的語法priblems:

首先,每次使用這是不正確的:

self.curRequest 

第一次只有你設置請求,使用self.request = blah

後,只需使用[curRequest xyz]

第二個嚴重的問題,

[self.curRequest setDidFailSelector:success_callback]; 
[self.curRequest setDidFinishSelector:failure_callback]; 

你忘了 '選擇' 位,它應該看起來更像是這樣的:

[request setDidFinishSelector:@selector(blahCommuniqueDone:)]; 
[request setDidFailSelector:@selector(blahCommuniqueDoneProblem:)]; 

(也要小心冒號。)忘記試圖通過冒號作爲SELs。這兩個例程就在同一個對象中。你只需指向他們。

下面是一個典型的完整工作示例。如果你遵循這個,你將沒有問題。這三個例程只需要在的同一個文件中就可以使用

-(void) blahCommunique 
    { 
    // it sends the string blah for pid (even if blank) 
    // from V5 it sends the blah as well .. 

    NSURL *url = [NSURL URLWithString:@"https://blah?blah"]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 

    [request setPostValue:blahAA forKey:@"blahAA"]; 
    [request setPostValue:blahBB forKey:@"blahBB"];// new in V5 

    [request setPostValue:SIMPLESTRINGTHISVERSION forKey:@"version"]; 

#define QQ(a) [NSString stringWithFormat:@"%d", a] 

    [request setPostValue:QQ(vfourblahCount) forKey:@"cr"]; 
    [request setPostValue:QQ(vfourblahCount) forKey:@"sb"]; 
    [request setPostValue:QQ(vfourblahCount) forKey:@"so"]; 
    [request setPostValue:QQ(vfourblahCount) forKey:@"lc"]; 
    [request setPostValue:QQ(OLDlaunchCount) forKey:@"olc"]; 

#undef QQ 

    [request setPostValue:[[NSLocale preferredLanguages] objectAtIndex:0] forKey:@"langpref"]; 

    [request setDelegate:self]; 
    [request setDidFinishSelector:@selector(statsCommuniqueDone:)]; 
    [request setDidFailSelector:@selector(statsCommuniqueDoneProblem:)]; 

    [request startAsynchronous]; 
    } 

-(void) statsCommuniqueDone:(ASIHTTPRequest *)request 
    { 
    NSData *newStringData = [request responseData];//todo, check [incomingFloatData length] 

    self.factoryMessageIfAny = 
     [[[NSString alloc] initWithData:newStringData encoding:NSUTF8StringEncoding] autorelease]; 

    if ([self.factoryMessageIfAny isEqualToString:@"OK"]) 
     self.factoryMessageIfAny = @""; 
    } 
-(void) statsCommuniqueDoneProblem:(ASIHTTPRequest *)request 
    { 
    // statsCommuniqueDoneProblem ... ! 
    self.factoryMessageIfAny = @""; 
    }