2011-11-24 66 views
0

我嘗試使用NSURLConnection從「http:// .......」下載(7kb)文件,但徒勞無益。 我聲明瞭所有委託(連接:)方法,但NSMutable對象包含0個字節。 這是爲什麼?無法在iphone模擬器中使用NSURLConnection下載文件

甚至NSLogs連接:方法沒有打印。

編輯:下面添加

.H

@interface URLConnect : NSObject { 

    NSMutableString *textView; 
    NSMutableData *responseData; 

} 
- (void)loadURL; 
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response; 
- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data; 
- (void)connectionDidFinishLoading:(NSURLConnection *)conn; 
- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error; 

@end 




.m file 

#import "URLConnect.h" 

@implementation URLConnect 


- (void)loadURL { 

    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://wordpress.org/extend/plugins/about/readme.txt"] 
            cachePolicy:NSURLRequestUseProtocolCachePolicy 
              timeoutInterval:60.0]; 

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

    if(theConnection){ 
     responseData = [[NSMutableData data]retain]; 
     NSLog(@"Connection starting: %@", theConnection); 
    } 


} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    NSLog(@"Recieved response with expected length: %i", [response expectedContentLength]); 
    [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    NSLog(@"Recieving data. Incoming Size: %i Total Size: %i", [data length], [responseData length]); 
    [responseData appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Succeeded! Received %d bytes of data",[responseData length]); 
    [connection release]; 
    NSLog(@"Connection finished: %@", connection); 
    NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 
    NSLog(@"%@",txt); 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [responseData release]; 
    [connection release]; 
    [textView setString:@"Unable to fetch data"]; 
} 

@end 
+2

你做錯了什麼?代碼在這裏會有所幫助。 :) –

+0

我已經添加上面的代碼 – chetan

+0

從頭文件中刪除以下函數聲明,因爲它們是http連接創建的委託。 (NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response;(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response; (無效)連接:(NSURLConnection *)conn didReceiveData:(NSData *)data; - (void)connectionDidFinishLoading:(NSURLConnection *)conn; - (void)連接:(NSURLConnection *)conn didFailWithError:(NSError *)error; 第二個原因可能是您已經使用單獨的類來生成連接。可能你有沒有分配內存到它的對象或使用diff對象來產生con –

回答

1

這是下載文件並將其保存在設備或模擬器(即上機)上的代碼...

這是沒有確切的代碼,但我希望它會幫助你....

@interface URLConnect : NSObject { 

NSMutableString *textView; 
NSMutableData *receivedData; 
NSURLRequest* DownloadRequest; 
NSURLConnection* DownloadConnection; 

long long bytesReceived; 
long long expectedBytes; 

} 
@property (nonatomic,retain) NSMutableData *receivedData; 
@property (nonatomic, readonly, retain) NSURLRequest* DownloadRequest; 
@property (nonatomic, readonly, retain) NSURLConnection* DownloadConnection; 


.m file 

#import "URLConnect.h" 

@implementation URLConnect 

// synthesize the properties 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [receivedData appendData:data]; 

    unsigned char byteBuffer[[receivedData length]]; 
    [receivedData getBytes:byteBuffer]; 
    NSLog(@"Data === %ld",receivedData); 

    NSInteger receivedLen = [data length]; 
    bytesReceived = (bytesReceived + receivedLen); 
    NSLog(@"received Bytes == %f",bytesReceived); 

    if(expectedBytes != NSURLResponseUnknownLength) 
    { 
     NSLog(@"Expected Bytes in if == %f",expectedBytes); 
     NSLog(@"received Bytes in if == %f",bytesReceived); 

     float value = ((float) (bytesReceived *100/expectedBytes))/100; 
     NSLog(@"Value == %f",value); 
    } 

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [connection release]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    expectedBytes = [response expectedContentLength]; 
    NSLog(@"%f",expectedBytes); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    // code for downloading file to device or simulator 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"AnyName.txt"]; 

    unsigned char byteBuffer[[receivedData length]]; 
    [receivedData getBytes:byteBuffer]; 

    [self.receivedData writeToFile:pdfPath atomically:YES]; 

    [DownloadConnection release]; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    NSURL *targetURL = [NSURL URLWithString:urlString]; 
    DownloadRequest = [NSURLRequest requestWithURL:targetURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:50.0]; 
    DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self]; 

    if (DownloadConnection) { 
     receivedData = [[NSMutableData data]retain]; 
    } 

    // display your textfile here 


} 
+0

嗨mAc這不是爲我工作...同樣的問題,在receivedData 0字節,我沒有在外面的「viewDidLoad」。 並沒有顯示日誌 – chetan

+0

爲什麼你還沒有viewDidLoad方法??? – mAc

+0

i dint得到什麼[super viewDidLoad]; does – chetan

0

這聽起來像你實際上並沒有設置你的NSURLConnection實例委託屬性的代碼。這可以解釋爲什麼委託方法中的NSLog未打印出來。

+0

我已經添加了代碼。請任何幫助 – chetan

+0

您是否收到任何警告或錯誤? –

+0

沒有警告/錯誤,但responseData的大小是0字節 – chetan

相關問題