Ich bin vor Netzwerkproblem oft wenn ich webservices.Each Zeit nannte es gibt mir Fehler wieNetzwerkfehler namens „NSURLErrorDomain-Code = -1005‚Die Netzwerkverbindung verloren.‘Kommt zu häufig
NSURLErrorDomain Code = -1005 wurde die Netzwerkverbindung verloren.
aber Netzwerk conenction ist noch vorhanden es gibt mir diese errors.Also diesen gleichen Fehler ist zu frequent.Can einer mir in this.Here helfen, den Code ich bin mit:
- (void)postRequest:(WSPostRequestType)servicetype parameters:(NSString*)parameters customeobject:(id)object block:(ResponseBlock)block
{
if (![self isNetworkAvailable])
{
if (errorAlertView != nil)
{
errorAlertView = [[UIAlertView alloc]initWithTitle:@"AppName" message:@"Network not reachable !!" delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[errorAlertView show];
}
return;
}
NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/******************* Set Url for WebService ********************/
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSString *URLString = [[NSString alloc]init];
switch (servicetype)
{
case WSUserRegistration:
URLString = URLRegistraion;
break;
default:
break;
}
//NSLog(@"url String :%@",URLString);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:DEFAULT_TIMEOUT];
[request setURL:[NSURL URLWithString:URLString]];
[request setHTTPMethod:@"POST"];
NSData *postData;
// NSData *jsonData;
NSString *postLength;
if (object == nil)
{
NSString *PostParamters= parameters;
postData = [PostParamters dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:postData];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
else {
postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[object length]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:object];
}
id Responceobjects;NSString *responseString;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (data.length>0)
{
responseString = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSDictionary *dictionary;
dictionary=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
switch (servicetype) {
case WSUserRegistration:
{
responseString=[NSString stringWithFormat:@"%@",[dictionary valueForKey:@"status"]];
Responceobjects=[dictionary valueForKey:@"data"];
}
break;
default:
break;
}
}
else if(error != nil)
{
NSLog(@" error from webservice parse line n0. 681 %@",error.localizedDescription);
if(error.code!=-1005 || ![error.localizedDescription isEqual:@"The network connection was lost."])
{
[AZNotification showNotificationWithTitle:error.localizedDescription controller:ROOTVIEW notificationType:AZNotificationTypeError];
}
}
else if(data.length == 0)
{
[AZNotification showNotificationWithTitle:@"Data Not Available..!!" controller:ROOTVIEW notificationType:AZNotificationTypeError];
}
dispatch_async(dispatch_get_main_queue(), ^(){
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (error)
{
@try
{
[ROOTVIEW dismissViewControllerAnimated:YES completion:nil];
}
@catch (NSException *exception) {
}
block(error,Responceobjects,responseString,nil);
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
}
else
{
block(error,Responceobjects,responseString,nil);
}
}];
});
});
}];
[operation setQueuePriority:NSOperationQueuePriorityVeryHigh];
[self.operationQueue addOperation:operation];
}
Hier ist Check Network Code verfügbar.
- (BOOL)isNetworkAvailable
{
if([[NetworkAvailability instance] isReachable])
{
return YES;
}
else
{
return NO;
}
return NO;
}
Meine Anwendung meist auf webservice data.So gibt es so viele Webservice Aufruf für verschiedene webservice.Thanks.
Was ist isNetworkAvailable .. geben Sie den Code. –
Überprüfen Sie, ob Sie http oder https verwenden? – Bharath