【问题标题】:NSURLConnectionDelegate methods not called using AFNetworking code未使用 AFNetworking 代码调用 NSURLConnectionDelegate 方法
【发布时间】:2014-08-27 00:03:21
【问题描述】:

我正在使用 AFNetworking,代码运行良好,但连接成功后没有调用 NSURLConnectionDelegate 方法的代理网络。

我已经在我的接口文件中声明为 NSURLConnection 的委托。但似乎没有任何工作,使用后台线程和主线程是否有问题? 这是我的代码:

NSLog(@"\n\n1 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));

AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];

 manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/html"];





 NSDictionary *parameters = @{
                             @"email": [Constants sharedInstance].email,

                             @"password": [Constants sharedInstance].password,

                             };

 NSLog(@"\n\nparameters:   %@",parameters);



 [manager POST:@"https://anyurl/" parameters:parameters success:^

 (AFHTTPRequestOperation *operation, id responseObject) {


   //  [operation setRunLoopModes:[NSSet setWithObject:NSDefaultRunLoopMode]];

     NSLog(@"\n\n 2 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));


     [self stopLoading];

     NSLog(@"\n\nconnection successful!!");

    NSLog(@"\n\nJSON: %@", responseObject);

 }

      failure:^(AFHTTPRequestOperation *operation, NSError *error) {

 NSLog(@"\n\nError: %@", error);

 [self stopLoading];


 NSLog(@"Connection could not be made");

 UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Network" message:@"Cannot make connection to the server" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

 [alert show];



      }
   ];

  NSLog(@"\n\n 3 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));

  [self startLoading];


  NSLog(@"\n\n4 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));

我看到了这个link,但无法使用那个答案。

【问题讨论】:

    标签: multithreading ios7 afnetworking-2 nsurlconnectiondelegate


    【解决方案1】:

    AFNetworking 是一个单独的库,如果你使用AFNetworking,它的成功和失败块将被执行。如果要执行NSURLConnectionDelegate 方法,请使用NSURLConnection 而不是AFNetwork

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多