嗨我試圖使用NSURLConnection委託方法獲取存儲在服務器上的屬性List到我的設備。iOS 5:pList的異步檢索 - 接收到的數據總是0字節
我在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連接:didReceiveResponse:] 183
fileFetcher.m - [fileFetcher連接:didReceiveData:] 190
接收數據是零
fileFetcher.m - [fileFetcher connectionDidFinishLoading:] 215
成功了!接收的0字節的數據
的有接收到一個對propertyList
流轉換數據具有字節太少
fileFetcher.m錯誤 - [fileFetcher connectionDidFinishLoading:] 236和
感謝。
receivedData不弱,是嗎? –
是的,它很弱。那就是那個神聖的問題。哇,它的奇怪,我花了大約1天的時間來調試這個東西! iOS 5的這些變化讓我瘋狂! 非常感謝。您可以「回答」我的問題,以便我可以接受接受您的答案。 – banditKing
添加了一個更詳細的答案,爲什麼會發生這種情況。 –