无法在iphone模拟器中使用NSURLConnection下载文件

时间:2011-11-24 05:19:25

标签: iphone nsurlconnection

我尝试使用NSURLConnection从“http:// .......”下载(7kb)文件,但是徒劳无功。 我声明了所有委托(连接:)方法,但NSMutable对象保存0个字节。 这是为什么?

甚至连接的NSLog:方法都没有打印出来。

编辑:添加以下代码

.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 :(得分: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


}

答案 1 :(得分:0)

听起来您实际上并未在NSURLConnection的实例上设置委托属性。这可以解释为什么委托方法中的NSLog没有打印出来。