您好我正在尝试使用NSURLConnection委托方法将存储在服务器上的属性列表添加到我的设备上。
我在Jeff LaMarche的iOS 3书中看到了一些教程,基本上我试图实现这些方法。
这些是实施方法
但我总是不断得到:“收到0字节的数据”我不明白:
#pragma -NSURLCOnnection Delegate Methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
[receivedData setLength:0];
}
//==============================================================================
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
if(!self.receivedData)
{
NSLog(@"Received Data was nil");
}
}
//==============================================================================
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert1 = [[UIAlertView alloc]
initWithTitle:@"Error"
message:[NSString stringWithFormat:@" Connection Failed! Error: %@ ,(URL:%@)", [error localizedDescription] ,[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert1 show];
NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
//Now take the data and convert it into a propertylist
NSData *plistData = self.receivedData;
NSPropertyListFormat format;
NSString *error;
id pList = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&error];
if(!pList)
{
NSLog(@"There was an error converting data received into a propertyList");
NSLog(error);
}
self.receivedData = nil;
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
}
-(void)retrieveFileFromServerAsynchronously
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
if(![self getURLToGetFileFrom])
{
UIAlertView *alert1 = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"InvalidPath"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert1 show];
}
if(![self getFileName])
{
UIAlertView *alert2 = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Invalid FileName"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert2 show];
}
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
NSMutableString *urlString = [[NSMutableString alloc] initWithString:[self getURLToGetFileFrom]];
[urlString appendString:[self getFileName]];
NSLog((@"Fetching file from URL %@", urlString));
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.00 ];
NSURLConnection *con = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if(con)
{
NSMutableData *data = [[NSMutableData alloc] init ];
self.receivedData = data;
if(!self.receivedData)
NSLog(@"Received Data was nil");
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
}
else
{
NSLog(@"%s %s %d", __FILE__, __FUNCTION__, __LINE__);
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Couldn't connect to remote server"
delegate:self cancelButtonTitle:@"Cancel"
otherButtonTitles:nil];
[alert show];
}
}
我在控制台上获得了上述输出。有人能告诉我从哪里开始解决这个问题吗?
fileFetcher.m - [fileFetcher connection:didReceiveResponse:] 183
fileFetcher.m - [fileFetcher connection:didReceiveData:] 190
收到的数据为零
fileFetcher.m - [fileFetcher connectionDidFinishLoading:] 215
成功了!收到0字节的数据
将收到的数据转换为propertyList
时出错
流有太少的字节
fileFetcher.m - [fileFetcher connectionDidFinishLoading:] 236
非常感谢。
答案 0 :(得分:0)
在retrieveFileFromServer中异步设置后,receiveData的最简单原因是nil,如果它被作为弱引用而被删除。
当您分配/初始化本地“数据”变量时,您创建一个强引用,但是然后将其分配给归零弱引用并从方法返回,导致“数据”超出范围,仅丢失强引用并导致弱引用receivedData被淘汰。