ios - NSURLConnection 发送异步请求 : not initiating request

标签 ios ios5

我有一些看起来应该可以尝试发送异步请求的代码,但从未生成请求:

函数如下:

-(void) setEmail: (NSString *) subject andBody: (NSString *) body
{
    NSString *urlString = @"http://www.my_url.com?";     

    // Create encoded query string
    NSString *query_string = @"subject=%@&body=%@";
    NSString *query_with_args = [NSString stringWithFormat:query_string , subject , body];     

    // Now encode the query
//    NSString *encodedQuery = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
//                                                                                           (__bridge CFStringRef)query_with_args,
//                                                                                           NULL,
//                                                                                           (CFStringRef)@"!*'();:@&=+$,/?%#[]",
//                                                                                           kCFStringEncodingUTF8);
//

    // Now concatinate url and query    
    NSString *final_url = [urlString stringByAppendingString:query_with_args];

    NSURL *url = [NSURL URLWithString:final_url];

    // Now send to the server    
    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

    // TODO: ok I dont really understand what this is
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];

    NSLog(@"......before request");

    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
     {         
         NSLog(@"On return");

         NSLog(@"This is data: %@" , data);
         NSLog(@"This is response: %@" , response);
         NSLog(@"This is error: %@" , error);

         NSLog(@"OK");         
     }];        
}

知道这里可能出了什么问题以及为什么从未发送请求吗?带有

的记录器

NSLog(@"……请求前");确实得到打印。

最佳答案

像这样尝试。您需要在其中实现 NSURLConnectionDelegate 协议(protocol)和方法。

NSString *final_url = [NSString stringWithFormat:@"http://www.my_url.com?subject=%@&body=%@",subject, body];
NSURL *url = [NSURL URLWithString:final_url];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if( theConnection )
    {
        webData = [NSMutableData data];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }
}

希望这对您有所帮助.. http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/

关于ios - NSURLConnection 发送异步请求 : not initiating request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11512427/

相关文章:

objective-c - iOS 5 中的单例?

ios - 在 TableViewCell Swift 中从 Firebase 加载图像

ios - SwiftUI 表单无法使用 Spacer() 正确定位

iphone - iOS:不正确/重复的 Facebook 请求消息

ios - 内联日期选择器的实现

twitter - 在 IOS5 中使用 TWrequest 将带有文本的图像发送到 Twitter

iphone - tabBarController 不显示

facebook - 如何让 UIWebView 打开 Facebook 登录页面以响应 iOS 5 和 iOS 6 上的 OAuth 请求?

ios - 如何使 UIViewController 半透明

iphone - 检测 UITextField 的焦点变化