2011-07-25 39 views
1

我正面临一点点的麻烦,发现我的代码有什么问题,因为我试图从不同的URL下载多个图片,并且请求无法通过。ASINetworkQueue请求allways失败 - ios

你们能帮我一下吗?

这里是我的代码:

// 
// Chapters.h 
// 
// 
// Created by Nuno Martins on 11/07/18. 
// Copyright 2011 WeTouch. All rights reserved. 
// 

#import <Foundation/Foundation.h> 
//#import <GHUnit/GHUnit.h> 
@class ASINetworkQueue; 


@interface Chapters : NSObject { 

NSString * chaptersBaseUrl; 
NSMutableArray * chaptersList; 



ASINetworkQueue *networkQueue; 


} 


@property (retain) ASINetworkQueue *networkQueue; 

@property (nonatomic, retain) NSString *chaptersBaseUrl; 
@property (nonatomic, retain) NSMutableArray *chaptersList; 



-(void)downloadChaptersIconsFromUrlArrayToFile:(NSMutableArray *)iconUrls; 


@end 


// 
// Chapters.m 
// 
// 
// Created by Nuno Martins on 11/07/18. 
// Copyright 2011 WeTouch. All rights reserved. 
// 

#import "Chapters.h" 
#import "Chapter.h" 
#import "PDFDataAgregator.h" 
#import "ASIHTTPRequest.h" 
#import "ASINetworkQueue.h" 

@implementation Chapters 

@synthesize chaptersBaseUrl; 
@synthesize chaptersList; 

@synthesize networkQueue; 


- (void)dealloc 
{ 
[networkQueue release]; 
[super dealloc]; 
} 

-(void)downloadChaptersIconsFromUrlArrayToFile:(NSMutableArray *)iconUrls 
{ 
    networkQueue = [[ASINetworkQueue alloc] init]; 
    // Stop anything already in the queue before removing it 
[networkQueue cancelAllOperations]; 

// Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking 
[networkQueue setDelegate:self]; 
[networkQueue setRequestDidFinishSelector:@selector(requestFinished:)]; 
[networkQueue setRequestDidFailSelector:@selector(requestFailed:)]; 
[networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; 


NSLog(@"Array-> %d", [iconUrls count]); 
NSMutableArray *myIcons = [[NSMutableArray alloc] initWithArray:iconUrls]; 



//Create Chapters Folder 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); 
NSString *docDirectory = [paths objectAtIndex:0]; 
NSString *newDir = [docDirectory stringByAppendingPathComponent:@"Chapters"]; 

[[NSFileManager defaultManager] createDirectoryAtPath:newDir withIntermediateDirectories:YES attributes:nil error: NULL]; 

for(unsigned i = 0; i < [myIcons count]; i++) 
{ 
    NSURL *url = [NSURL URLWithString:[myIcons objectAtIndex:i]]; 
    NSString *fileName = [url lastPathComponent]; 

    NSString *filePath = [newDir stringByAppendingPathComponent:fileName]; 

    NSLog(@"Icon File Path: %@",filePath); 


    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[iconUrls objectAtIndex:i]]]; 
    [request setDownloadDestinationPath:filePath]; 
    //[request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; 
     [request setTemporaryFileDownloadPath:[filePath stringByAppendingPathExtension:@"download"]]; 
     [request setAllowResumeForFileDownloads:YES]; 

     [networkQueue addOperation:request]; 



    } 


    [networkQueue go]; 
} 

- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    // You could release the queue here if you wanted 
    if ([networkQueue requestsCount] == 0) { 

    // Since this is a retained property, setting it to nil will release it 
     // This is the safest way to handle releasing things - most of the time you only ever need to release in your accessors 
     // And if you an Objective-C 2.0 property for the queue (as in this example)  the accessor is generated automatically for you 
     [self setNetworkQueue:nil]; 
    } 

    //... Handle success 
    NSLog(@"Request finished"); 
} 

- (void)requestFailed:(ASIHTTPRequest *)request 
{ 

    // You could release the queue here if you wanted 
    NSLog(@"Number of requests in Queue %d", [networkQueue requestsCount]); 

    if ([networkQueue requestsCount] == 0) { 
     [self setNetworkQueue:nil]; 
    } 

    //... Handle failure 
    NSLog(@"Request failed"); 
} 


- (void)queueFinished:(ASINetworkQueue *)queue 
{ 
    // You could release the queue here if you wanted 
    if ([networkQueue requestsCount] == 0) { 
     [self setNetworkQueue:nil]; 
    } 
    NSLog(@"Queue finished"); 
} 
+0

它给这个错误:描述错误错误域= ASIHTTPRequestErrorDomain代码= 6 “无法启动HTTP连接” 的UserInfo = 0x94067f0 {NSLocalizedDescription =无法启动HTTP连接} – nmartins

+0

https://gist.github.com/150447 –

回答

5

嗯,这是不好的URL格式相关的问题。

我是路过的http:/somesite.com/someimage.png强似http://somesite.com/someimage.png

我失踪了/因为当我使用stringByAppending路径追加BASEURL字符串的文件名部分它消除了一个斜线HTTP://。

现在解决!

+1

UUUhhhhhh !正确! – rptwsthi