“ - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data”not called

时间:2009-08-07 10:56:20

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

谢谢大家。

4 个答案:

答案 0 :(得分:4)

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

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

将无效,因为当connection超出范围时,connection将在包含方法的末尾发布。确保NSURLConnection *connectionNSMutableData *data被声明为成员变量,无论您执行此操作,还是allocinit。我的代码通常如下:

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

另外,release dealloc中的连接和数据。为了更好地衡量,release并在nildidFailWithError的最后将它们设置为didFinishLoading

[connection release];
connection = nil;
[data release];
data = nil;
祝你好运;我已经做了一百万次,如果你不能让它工作,请告诉我。

答案 1 :(得分:2)

你碰巧在线程中调用NSConnection吗?如果你正在发生的事情是线程在NSConnection及其代表完成之前终止,所以它只会弹出而没有错误。

解决方法是this线程

答案 2 :(得分:0)

您在didFailWithError中没有收到任何错误消息?有点愚蠢的建议,但你确定你正在设置合适的NSURLConnection代表吗?

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

有时它会像那样小。

另一个想法是放入像ASIHTTPRequest这样的工具包,看看它是否有效。

答案 3 :(得分:0)

如果尝试从另一个线程启动NSURLConnection,也可能会出现问题。 如果你没有为它定制Run Loop,请在主线程上调用方法[connection start]。

相关问题