我发现我的代码出现问题时遇到了一些麻烦,因为我试图从不同的网址下载多个图片,而且请求总是失败。
你们可以给我一些帮助吗?这是我的代码:
//
// 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 :(得分:5)
这是与Bad url格式相关的问题。
我正在传递http:/somesite.com/someimage.png而不是传递http://somesite.com/someimage.png
我错过了/因为当我使用stringByAppending路径将一个BaseUrl字符串附加到文件名时,它会删除一个HTTP://的斜杠。
现在解决了!