我已閱讀所有類似的問題,但沒有一個答案爲我工作。因此,讓我給你解釋一下什麼是我的問題: 我使用NSURLConnection的發送異步GET請求到我的後端側。我有時會使用正確的響應,但大多數情況下,我從GET請求中獲取最後一次獲取的結果(所以我認爲它必須是URL緩存)。異步NSURLConnection的有時沒有達到服務器端
這是我的代碼中,我創建和發送URL請求:
NSMutableURLRequest *URLRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: URL] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:kRequestTimeOut];
[URLRequest setHTTPMethod: @"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
if ([NSURLConnection canHandleRequest:URLRequest]) {
[NSURLConnection sendAsynchronousRequest: URLRequest
queue: queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
[queue release];
IPBetaLog(@"RESPONSE FROM %@: %@",URL,[[[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding] autorelease]);
IPBetaLog(@"RESPONSE CODE: %i",[(NSHTTPURLResponse*) response statusCode]);
if ([data length] > 0) {
int responseCode = 0;
if ([response isKindOfClass: [NSHTTPURLResponse class]]) {
responseCode = [(NSHTTPURLResponse*) response statusCode];
IPBetaLog(@"RESPONSE CODE: %i",responseCode);
switch (responseCode) {
case 200:
IPLog(@"Received message 'OPERATION_COMPLETED'");
break;
case 201:
IPLog(@"Received message 'OPERATION_COMPLETED'");
break;
case 202:
IPLog(@"Received message 'OPERATION_COMPLETED'");
break;
default:
return block(NO,nil,[self errorWithResponseCode: responseCode]);
break;
}
}
NSError *jsonError = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error:&jsonError];
NSLog(@"Result is %@", result);
if (jsonError != nil) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"JSON error", @"") forKey: NSLocalizedDescriptionKey];
[userInfo setObject: jsonError.localizedFailureReason forKey: NSLocalizedFailureReasonErrorKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorJSONError userInfo: userInfo]);
} else {
return block(YES,result,nil);
}
}
else if ([data length] == 0 && error == nil) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"Response is empty", @"") forKey: NSLocalizedDescriptionKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorEmptyReponse userInfo: userInfo]);
}
else if (error != nil && error.code == 1001) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"Request timed out", @"") forKey: NSLocalizedDescriptionKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorTimedOut userInfo: userInfo]);
}
else if (error != nil) {
return block(NO,nil,error);
}
}];
}
else {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject: NSLocalizedString(@"No Connection", @"Error message displayed when not connected to the Internet.") forKey: NSLocalizedDescriptionKey];
return block(NO,nil,[NSError errorWithDomain: MyErrorDomain code: IPRequestErrorBadRequest userInfo: userInfo]);
}
我使用的CachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData,嘗試過其他枚舉的高速緩存策略,但問題依然存在。 爲了證明自己問題出在iOS上,我已經成功地從Android和Rest Console發送了相同的請求,並且它每次都有效。
從我的後端側,從iOS的請求只配備有時,這是該情況下,當結果數據還是不錯的。 例如:我送與URL GET請求到我的服務器端,並請求到來時,我做出響應,併到達了iOS與響應數據。我向同一個URL發送另一個相同的GET請求,但服務器端沒有請求,並且iOS向我提供與先前請求相同的響應數據。我很少成功地到達服務器端。真的不知道爲什麼。 iPhone和服務器端之間沒有網絡問題。
請,如果您有任何解決方案。 非常感謝您在我的問題上的時間。
我都試過,但沒有幫助。方法POST,PUT,DELETE工作正常,但GET運行不正常 - 沒有到達服務器。 – Toni