Здравствуйте, я пытаюсь получить список свойств, хранящихся на сервере, на моем устройстве с помощью методов делегата NSURLConnection.
Я видел несколько руководств в книге Джеффа Ламарша по 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: didReceiveResponse:] 183
fileFetcher.m - [соединение fileFetcher: didReceiveData:] 190
Полученные данные были равны нулю
fileFetcher.m - [fileFetcher connectionDidFinishLoading:] 215
Преемник! Получено 0 байт данных
Произошла ошибка при преобразовании полученных данных в список свойств
В потоке было слишком мало байтов
fileFetcher.m - [fileFetcher connectionDidFinishLoading:] 236
Большое спасибо.