【问题标题】:asi-http-request asynchronous selector errorasi-http-request 异步选择器错误
【发布时间】:2010-12-02 16:32:37
【问题描述】:

当我使用选择器注册成功和失败的回调时,我在使用 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 {

[自我设置:@"/users/register" 成功回调:成功回调 失败回调:失败回调 ];

// 设置方法的输入 [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]; }

成功和失败回调

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

我像这样调用注册方法:

[self.appdelegate.userModel register_user:self.emailAddress.text 密码:self.password.text 确认密码: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'

感谢所有帮助,非常感谢:)~

【问题讨论】:

    标签: iphone objective-c asihttprequest


    【解决方案1】:

    Dudester,你有两个严重的语法问题:

    首先,这个的每次使用都是不正确的:

    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:)];
    

    (也要小心冒号。)忘记尝试将它们作为 SEL 传递。这两个例程就在同一个对象中。您只需指向它们即可。

    这是一个典型的完整工作示例。如果你遵循这个,你就没有问题了。这三个例程只是在同一个文件中

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

    【讨论】:

      猜你喜欢
      • 2011-06-03
      • 1970-01-01
      • 2021-07-18
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多