【问题标题】:"-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data" not called“-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data”未调用
【发布时间】:2009-08-07 10:56:19
【问题描述】:

看看这段代码 sn-p:-

-(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 对象。 这个问题似乎很愚蠢,但任何有答案的人....

谢谢大家。

【问题讨论】:

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


    【解决方案1】:

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

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

    另外,releasedealloc 中的连接和数据。为了更好地衡量,release 并将它们设置为nildidFailWithErrordidFinishLoading 的最后:

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

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

    【讨论】:

    • 谢谢米奇,但我仍然有同样的问题,它不工作。我在浏览器中测试了 URL,它在那里加载。你能帮帮我吗?
    • 不会因为指向它的变量超出独家新闻而发布任何内容!连接对象不需要保留,应该在完成工作后在委托的回调中释放。
    【解决方案2】:

    您不会碰巧在线程中调用 NSConnection 吗?如果你是,那么正在发生的事情是线程在 NSConnection 及其委托完成之前终止,所以它只会在没有错误的情况下爆炸。

    解决方法是在this 线程中

    【讨论】:

      【解决方案3】:

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

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

      有时候就是这么小。

      另一个想法是放入像ASIHTTPRequest 这样的工具包,看看它是否可以通过它们。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-07-21
          • 2012-10-16
          • 2014-08-07
          • 2022-12-28
          • 2012-06-27
          • 1970-01-01
          • 1970-01-01
          • 2020-12-11
          相关资源
          最近更新 更多