Iphone &引用-连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据“;不打电话
请查看此代码段:-Iphone &引用-连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据“;不打电话,iphone,web-services,iphone-sdk-3.0,soap,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 { N
-(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服务时,它工作得很好。但当我尝试时,问题就出现了
通过我的密码点击它。
一切正常,但未调用连接:didReceiveData
。
因此,webData
变量中没有数据。此webData
是一个NSMutableData
对象。
这个问题似乎很愚蠢,但任何一个有答案的人
谢谢大家。您没有在线程中调用NSConnection,是吗?如果是,那么发生的情况是,线程在NSConnection及其委托完成之前终止,因此它将在没有错误的情况下爆炸
解决方法是在线程中您在
didFailWithError
中也没有收到任何错误消息?这是一个愚蠢的建议,但您确定您正在设置正确的NSURLConnection
委托吗
NSURLConnection* connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
有时候是这样的小东西
另一个想法是访问一个类似于
ASIHTTPRequest
的工具包,看看通过它们是否有效。我怀疑您有内存管理问题。我可能在这一点上弄错了,但我相信:
NSURLConnection* connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
不会工作,因为当连接
超出范围时,连接
将在包含方法的末尾释放。确保在执行此操作时,将NSURLConnection*connection
和NSMutableData*data
声明为成员变量,并确保alloc
和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];
}
另外,释放中的连接和数据。为便于测量,释放
,并在didFailWithError
和didfishloading
的最后将其设置为nil
:
[connection release];
connection = nil;
[data release];
data = nil;
祝你好运;我已经做了一百万次了,如果您无法让它工作,请告诉我。如果您试图从另一个线程启动NSURLConnection,也可能会出现问题。
如果您没有为其定制运行循环,请在主线程上调用方法[connection start]。谢谢Mickey,但我仍然有同样的问题,它不起作用。我在浏览器中测试了URL,并将其加载到那里。你能帮帮我吗?不会因为一个指向它的变量从独家新闻中泄露而发布任何东西!连接对象不需要保留,应该在完成其作业后在委托的回调中释放。