2015-10-05 44 views
-1

Objective-C的問題,我最近從sendSynchronousRequest切換到dataTaskWithRequest與dataTaskWithRequest

與sendSynchronousRequest我的方法是可以正常使用,但是當我切換到dataTaskWithRequest我得到以下錯誤:

error NSURLError * domain: @"NSURLErrorDomain" - code: 4294966096 0x15ee96c0 

myError NSError * domain: nil - code: 1684370017 0x26cce125 

我不明白爲什麼。

這裏是舊代碼(註釋)和新代碼:

/*-(NSDictionary *)GetProductionScheduleData:(NSString *)areaDescription 
{ 
    NSString *areaDescriptionWSpaceCharacters = [areaDescription stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSString *requestString = [NSString stringWithFormat:@"%@?areaDescription=%@",kIP,areaDescriptionWSpaceCharacters]; 
    NSURL *JSONURL = [NSURL URLWithString:requestString]; 
    NSURLResponse* response = nil; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL]; 
    NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
    if(data == nil) 
     return nil; 
    NSError *myError; 
    NSDictionary *productionSchedule = [[NSDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]]; 
    return productionSchedule; 
}*/ 

-(void)GetProductionScheduleData:(NSString *)areaDescription Completetion:(void (^) (NSMutableDictionary * result,NSError * error))completion{ 

    NSString *areaDescriptionWSpaceCharacters = [areaDescription stringByReplacingOccurrencesOfString:@" " withString:@"%20"]; 
    NSString *requestString = [NSString stringWithFormat:@"%@?areaDescription=%@",kIP,areaDescriptionWSpaceCharacters]; 
    NSURL *JSONURL = [NSURL URLWithString:requestString]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL]; 
    NSURLSession *session = [NSURLSession sharedSession]; 
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request 
               completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
             { 
              NSError *myError; 
              NSMutableDictionary *productionSchedule = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]]; 

              completion(productionSchedule,myError); 

             }]; 
    [dataTask resume]; 

} 

請幫助!這與sendSynchronousRequest一起工作我開始不喜歡dataTaskWithRequest。

+0

您使用的是ios9嗎?我得到了同樣的錯誤,當我改變到ios9 swift2,也許ATS是問題 – user2258959

回答

0

您擁有的NSURLSession代碼是正確的,我使用有效的URL進行了確認。

在試圖對JSON進行解析之前,您停止檢查數據是否爲零。如果您添加該檢查,我敢打賭你會發現有一個錯誤,實際上數據爲零。

更改爲:

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { 
    // handle request error 
    if (error) { 
     completion(nil, error); 
     return; 
    } 

    NSError *myError; 
    NSMutableDictionary *productionSchedule = [[NSMutableDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]]; 
    completion(productionSchedule,myError); 
}]; 

我會建議試圖將其設置爲productionSchedule(這也可能導致崩潰)之前還檢查myError。

+0

這沒有工作:( – user979331

+0

你可以提供更多的信息,什麼沒有工作?這將是有益的,如果你更新發布的代碼瓦特/所做的更改也可以提供您請求的完整URL。 – Casey

相關問題