简体   繁体   English

未调用NSURLConnection异步委托方法

[英]NSURLConnection Asynchrone delegate method not called

I'm developing a WebService. 我正在开发WebService。 And i'm developing my iOS client with NSURL. 我正在用NSURL开发我的iOS客户端。 I send asynchronous request to server and got data (packet by packet). 我向服务器发送异步请求并获取数据(逐个数据包)。 My request is sent to server correctly, but no delegate method are called. 我的请求已正确发送到服务器,但未调用任何委托方法。

@implementation WebServiceClientCom

...

-(void) connectionDidFinishedLoading:(NSURLConnection*) in_urlConnection
{
    int i=0;
}

-(void) connection:(NSURLConnection*) connection didReceiveResponse:(NSURLResponse *)response

{
    NSLog(@"YOUPI");
    int i=0;
}

-(void) connection:(NSURLConnection*) connection didReceiveData:(NSData *)data

{
    NSLog(@"YOUPI");
    int i=0;
}

-(void) connection:(NSURLConnection*) connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite;

{
    NSLog(@"YOUPI");
    int i=0;
}

-(void) connection: (NSURLConnection*)connection didFailWithError: (NSError*) error
{
    NSLog(@"%@", error);
    int i=0;
}

-(void) sendAsynchronousRequest: (NSData*) in_dataToSend
{
    _oUrlRequest.HTTPBody = in_dataToSend;
    _oUrlConnection = [[NSURLConnection alloc] initWithRequest:_oUrlRequest delegate:self startImmediately:YES];
    if(!_oUrlConnection)
        NSLog(@"Connection failed");
    else
        NSLog(@"Connection succeeded");
}

@end

I don't understand what is the problem? 我不明白是什么问题? Why? 为什么? Where? 哪里?

Help me please :) 请帮帮我 :)

You probably forgot to set the delegate, make sure you do not pass nil as parameter: 您可能忘记设置委托,请确保不要将nil作为参数传递:

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

for more information take a look at using NSURLConnection 有关更多信息,请查看使用NSURLConnection

UPDATE 更新

Try: 尝试:

_oUrlConnection = [[NSURLConnection alloc] initWithRequest:_oUrlRequest delegate:self  startImmediately:NO];
[_oUrlConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
[_oUrlConnection start];

Maybe it is being executed in another thread. 也许它正在另一个线程中执行。

If you are not holding a strong reference to your WebServiceClientCom object, it could be thrown out before you receive a response. 如果您对WebServiceClientCom对象的引用不强,则在收到响应之前可能会将其丢弃。 Try this where you're using this class instance instead: 在使用此类实例的地方尝试以下操作:

@interface MyClass
@property (strong) WebServiceClientCom *myCommunicator;
/* ... */
@end

@implementation MyClass
- (void)communicate:(NSData *)data {
  if (!self.myCommunicator) {
    self.myCommunicator = [[WebServiceClientCom alloc] init];
  }
  [self.myCommunicator sendAsynchronousRequest:data];
}
@end

Ok, Thansk all, I just forgot [[NSRunLoop currentRunLoop] run] after my connection [[NSURLConnection alloc] intiWithRequest:request delegate:self startImmediately:YES] 好吧,谢谢大家,我只是在连接[[NSURLConnection alloc] intiWithRequest:request delegate:self startImmediately:YES]之后忘记了[[NSRunLoop currentRunLoop] run] [[NSURLConnection alloc] intiWithRequest:request delegate:self startImmediately:YES]

But, I've got another problem: How can i quit this loop and so the asynchronous method??? 但是,我还有另一个问题:我如何才能退出此循环,从而退出异步方法??? ^^ ^^

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM