1

我在使用NSMutableURLRequest时工作正常

- (void)postAsynchronousOnQueue:(NSOperationQueue*)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))completionHandler {
    NSURLRequest* request = [self createPostRequest];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:completionHandler];
}
-(NSURLRequest*)createPostRequest {
        NSMutableURLRequest* request = [[HttpRequest requestWithRelativePath:@"/photo"] toNSMutableURLRequest];
             [request setHTTPMethod:@"POST"];
             [request setHTTPBody:[self encodeParamsForUpload]];
             return request;  
}

但问题是当我的应用程序处于后台模式时,它将无法工作。

这是我的HttpRequest课堂方法:

+(id)requestWithRelativePath:(NSString*)docpath {
    return [[HttpRequest alloc] initWithRelativePath:docpath server:server username:email password:password];
}  
-(id)initWithRelativePath:(NSString*)docpath server:(NSString*)server username:(NSString*)username password:(NSString*)password {
    if (self = [super init]) {
        docpath = [docpath stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
        _request = [self createRequestWithDocPath:docpath server:server username:username password:password];
    }
    return self;
}

- (NSMutableURLRequest*)createRequestWithDocPath:(NSString*)docpath server:(NSString*)server username:(NSString*)username password:(NSString*)password {
    NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"https://%@%@", server, docpath]];
    NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setTimeoutInterval:120.0];
    if ((username != nil) && (password != nil)){
        NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, password];
        NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];
        NSString *authValue = [NSString stringWithFormat:@"Basic %@", [self base64Encoding:authData]];
        [request setValue:authValue forHTTPHeaderField:@"Authorization"];
    }
    return request;
}

从堆栈溢出,我发现NSURLSession在后台进行 API 调用。所以我用NSURLSession. 这是我所做的更新代码:

- (void)postAsynchronousOnQueue:(NSOperationQueue*)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))completionHandler {
    NSURLRequest* request = [self createPostRequest];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:completionHandler];
}
-(NSURLRequest*)createPostRequest {
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration ephemeralSessionConfiguration];
    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

    NSMutableURLRequest* request = [[HttpRequest requestWithRelativePath:@"/photo"] toNSMutableURLRequest];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[self encodeParamsForUpload]];
    //Create task
    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //Handle your response here
        [[NSURLSession sharedSession]invalidateAndCancel];
    }];

    [dataTask resume];
    return request; 
}

但是,当我使用NSURLSession请求发送两次时,我已经将断点NSMutableURLRequest排成一行,但它只调用一次。

请帮我解决问题。

4

1 回答 1

0

createPostRequest正在创建一个请求并通过NSURLSession. 但它也会返回NSURLRequestpostAsynchronousOnQueue继续再次提交,这次是通过 deprecated NSURLConnection

删除NSURLConnection代码并仅依靠NSURLSession发出请求。


例如:

- (void)postAsynchronousOnQueue:(NSOperationQueue*)queue completionHandler:(void (^)(NSData *, NSURLResponse*, NSError*))completionHandler {
    NSURLSession *defaultSession = [NSURLSession sharedSession];

    NSMutableURLRequest* request = [[HttpRequest requestWithRelativePath:@"/photo"] toNSMutableURLRequest];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[self encodeParamsForUpload]];

    NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //Handle your response here
        [queue addOperationWithBlock:^{
            completionHandler(data, response, error);
        }];
    }];

    [dataTask resume]; 
}
于 2016-07-19T09:45:41.477 回答