【发布时间】:2016-07-26 09:50:26
【问题描述】:
每当我调用 webservices 时,我都会多次遇到网络问题。每次它都会给我类似的错误
NSURLErrorDomain Code=-1005 网络连接丢失。
但是网络连接仍然存在,它给了我这些错误。同样的错误也太频繁了。任何人都可以帮助我。这是我正在使用的代码:
- (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];
}
这里是检查网络可用代码。
- (BOOL)isNetworkAvailable
{
if([[NetworkAvailability instance] isReachable])
{
return YES;
}
else
{
return NO;
}
return NO;
}
我的应用程序主要基于 webservice 数据。所以有很多 webservice 调用不同的 webservice。谢谢。
【问题讨论】:
-
什么是isNetworkAvailable..给出代码。
-
查看你使用的是http还是https?
标签: ios objective-c web-services networking