iphone - "-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data"未调用

标签 iphone web-services iphone-sdk-3.0 soap

看看这个代码片段:-

-(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 访问网络服务时,它工作正常。但当我尝试时问题出现了 通过我的代码击中它。 一切正常,但 connection:didRecieveData 没有被调用。 因此,我在 webData 变量中没有得到任何数据。这个 webData 是一个 NSMutableData 对象。 这个问题似乎很愚蠢,但任何有答案的人......

谢谢大家。

最佳答案

我怀疑您遇到了内存管理问题。我可能会弄错,但我相信:

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

不起作用,因为当connection超出范围时,connection将在包含方法的末尾被释放。确保在执行此操作时将 NSURLConnection *connectionNSMutableData *data 声明为成员变量,并且分配init 适本地。我的代码通常如下所示:

    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中的连接和数据。为了更好地衡量,release 并在 didFailWithErrordidFinishLoading 的最后将它们设置为 nil:

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

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

关于iphone - "-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data"未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1244137/

相关文章:

ios - obj-c 图像自动调整大小...?

c# - 为什么 ASP.NET session 的过期时间早于其超时时间?

wcf - 安全的 WCF 服务

frameworks - iphone开发中的麻雀框架是什么?

email - iPhone SDK : Send Email in background in iphone app

iphone - 分发 iPhone 特别更新

ios - sleep 周期 - 这是哪种后台模式?

java - JAX-RS - 在 RESTful Web 服务中进行身份验证并获取用户数据

iphone - 帮助我自动化应用商店提交流程

iphone - 为回合制棋盘游戏编写 AI