1

从 wifi 切换到 3g后,我遇到了一个奇怪的问题,Reachability 和 NSURLConnection 都显示错误(没有互联网连接/网络连接丢失)。尽管切换到 3g 后所有应用程序都运行良好。我仔细检查了应用程序和设备的移动数据是否打开。重新安装应用程序它工作正常,请帮助。这是一些可达性代码:

-(BOOL)toCheckNetworkStatus
{
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];

NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

if(internetStatus == NotReachable ){
    [self displayToastWithMessage:@"Connection lost!" AndSubtitle:@"Please check your network connection." delay:3.2];
    return  NO;
}
else{
    return YES;
}
}

NSURLConnection 的代码:

    NSError *error;
    NSData *postData = [NSJSONSerialization dataWithJSONObject:dictionary
                                                       options:0
                                                         error:&error];
    if (error)
        NSLog(@"Failure to serialize JSON object %@", error);


    NSURL *url = [NSURL URLWithString:palURL];
    NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:TIMEOUTINTERVAL];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPBody:postData];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:
     ^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (data)
         {
             NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
             NSLog(@"UPLOADED LOCATION RESPONSE = %@",responseDict);
         }
         else
         {
             NSLog(@"connectionError===%@",connectionError);
         }
     }];
4

0 回答 0