开发者

asi-http-request asynchronous selector error

I keep getting a very weird error when using asi-http-request when I use selectors to register callbacks for success and failure.

The following methods are used:

//  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:succes开发者_运维技巧s_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];

}

The register method

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

[self setup:@"/users/register" successCallback:success_callback failureCallback:failure_callback ];

// Setup the input to the method [self.curRequest setPostValue:email forKey: @"email"]; [self.curRequest setPostValue:password forKey: @"password"]; [self.curRequest setPostValue:confirm_password forKey: @"confirm_password"];

NSLog(@"We got here yo :)~ %@ %@ %@", email, password, confirm_password);

[self.curRequest startAsynchronous]; }

Success and Failure Callbacks

- (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]);
}

I invoke the register method like so:

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

The callstack and error from 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'

I appreciate any and all help, thanks alot :)~


Dudester you have two serious syntax priblems:

First, each use of this is incorrect:

self.curRequest

The first time only you set request, use self.request = blah.

After that, just use [curRequest xyz]

Second serious problem,

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

You forgot the 'selector' bit, it should look more like this:

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

(Be careful with the colons, also.) Forget about trying to pass them in as SELs. The two routines are right there in the same object. You simply point to them.

Here's a typical complete working example. If you follow this you'll have no problems. These three routines just sit in the same file.

-(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 = @"";
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜