名为" NSURLErrorDomain Code = -1005"网络连接丢失的网络错误。"过于频繁

时间:2016-07-26 09:50:27

标签: ios objective-c web-services networking

每当我打电话给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.Thanks。

2 个答案:

答案 0 :(得分:0)

重启模拟器并检查。如果它无效,请重置模拟器并检查。

答案 1 :(得分:0)

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

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

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

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