首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLConnection sendAsynchronousRequest:未发起请求

NSURLConnection sendAsynchronousRequest:未发起请求
EN

Stack Overflow用户
提问于 2012-07-17 04:56:39
回答 1查看 1.3K关注 0票数 1

我有一些代码,它看起来应该可以工作,试图发送一个异步请求,但请求永远不会生成:

下面是函数:

代码语言:javascript
复制
-(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(@"......before request");会打印出来。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-17 05:16:21

试着这样..。您需要在其中实现NSURLConnectionDelegate协议和方法。

代码语言:javascript
复制
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/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11512427

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档