asi-http-request异步选择器错误 [英] asi-http-request asynchronous selector error

查看:101
本文介绍了asi-http-request异步选择器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用选择器注册成功和失败的回调时,使用asi-http-request时,我总是收到一个非常奇怪的错误.

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

使用以下方法:

//  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 setup:@"/users/register" 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"];

// 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(@我们到这里了:)〜%@%@%@",电子邮件,密码,确认密码);

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

[self.curRequest startAsynchronous]; }

[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方法:

I invoke the register method like so:

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

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

来自gdb的调用堆栈和错误:

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

您设置请求的 仅初次使用 ,请使用self.request = blah.

之后 ,只需使用[curRequest xyz]

第二个严重问题,

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

(也要注意冒号.)不要试图将它们作为SEL传递.这两个例程就在同一对象中.您只需指向他们.

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

这篇关于asi-http-request异步选择器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆