开发者

ASINetworkQueue requests always fails - ios

I'm facing a little bit of trouble finding whats wrong with my code, because I'm trying to download several images from different urls and the requests are always failing.

Could you guys give me a little help?

Here is my code:

开发者_JAVA技巧
//
//  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");
}


Well this was a problem related with Bad url format.

I was passing http:/somesite.com/someimage.png instead of passing http://somesite.com/someimage.png

I was missing the / because when I append a BaseUrl string to the filename using stringByAppending path Component it removes one slash of the HTTP://.

Solved now!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜