&quot ;-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data"不叫 [英] "-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data" not called

查看:114
本文介绍了&quot ;-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data"不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看一下这段代码: -

Have a look to this code snippet:-

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{               
    [webData setLength: 0];           
}



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

{
    NSLog(@"Recieving Data...");
    [webData appendData:data];

}



-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"ERROR with theConenction");
    [connection release];
    [webData release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);
    NSLog(theXML);


}   

我正在调用SOAP Web服务。我的代码中没有显示错误或警告。
当我通过safari访问网络服务时,它工作正常。但是当我尝试
通过我的代码点击时出现问题。
一切正常,但连接:didRecieveData 未被调用。
因此,我在 webData 变量中没有数据。此 webData 是一个 NSMutableData 对象。
问题似乎很愚蠢,但任何人都有任何答案....

I am calling a SOAP web service.There are no errors or warnings displayed in my code. When I hit the web service through safari it works fine. But the problem arises when I try hit it through my codes. Everything works fine but the connection:didRecieveData does not gets called. Thus, I get no data in the webData variable. This webData is a NSMutableData object. The problem seems to be silly but any one with any answers ....

谢谢大家。

推荐答案

我怀疑你遇到了内存管理问题。我可能会对此有误,但我相信即使:

I suspect you are having a memory management issue. I could be mistaken on this, but I believe that even:

NSURLConnection* connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

不起作用,因为连接将当连接超出范围时,在包含方法结束时释放。确保 NSURLConnection * connection NSMutableData * data 被声明为成员变量正在这样做,你 c>分配和 init 。我的代码通常如下所示:

won't work, because connection will be released at the end of the containing method, when connection goes out of scope. Make sure NSURLConnection *connection and NSMutableData *data are declared as member variables where ever you are doing this, and that you alloc and init them appropriately. My code usually looks like:

    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                          timeoutInterval:30.0];
    // cancel any old connection
    if(connection) {
        [connection cancel];
        [connection release];
    }
    // create new connection and begin loading data
    connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    if(connection) {
        // if the connection was created correctly, release old data (if any), and alloc new
        [data release];
        data = [[NSMutableData data] retain];
    }

此外,发布 dealloc 中的连接和数据。为了更好的衡量,发布并将它们设置为 nil didFailWithError的最后 didFinishLoading

Also, release the connection and data in dealloc. For good measure, release and set them to nil at the very end of didFailWithError and didFinishLoading:

[connection release];
connection = nil;
[data release];
data = nil;

祝你好运;我已经做了一百万次了,如果你不能让它工作,请告诉我。

Good luck; I've done this a million times, let me know if you cannot get it working.

这篇关于&quot ;-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data"不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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