【问题标题】:Network error named "NSURLErrorDomain Code=-1005 "The network connection was lost." comes too frequently名为“NSURLErrorDomain Code=-1005“网络连接丢失。”的网络错误来得太频繁了
【发布时间】: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


【解决方案1】:

您必须像以下方式重新启动内容和设置:

iOS Simulator -> Reset Content and Settings -> Press Reset 

通过使用它,您的问题将得到解决。

不要使用HTTPMethod,而是尝试使用HTTPBody。解决这个问题后,我希望一切都会好起来。

【讨论】:

    【解决方案2】:

    重启模拟器并检查。如果它不起作用,请重置您的模拟器并检查。

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 2019-10-12
      • 2023-03-10
      • 2016-02-22
      • 2017-11-06
      • 2014-10-11
      • 2017-05-07
      • 2014-09-03
      • 2017-03-08
      相关资源
      最近更新 更多