看看这个代码片段:-
-(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
is a NSMutableData
目的。
这个问题似乎很愚蠢,但任何有答案的人......
谢谢你们。
我怀疑你有内存管理问题。我可能会弄错,但我相信:
NSURLConnection* connection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
行不通,因为connection
将在包含方法结束时释放,当connection
超出范围。确保NSURLConnection *connection
and NSMutableData *data
被声明为成员变量无论你在哪里做这件事,并且你alloc
and 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];
}
Also, release
连接和数据在dealloc
。为了更好地衡量,release
并将它们设置为nil
在最后didFailWithError
and didFinishLoading
:
[connection release];
connection = nil;
[data release];
data = nil;
祝你好运;我已经这样做了一百万次,如果你无法让它工作,请告诉我。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)