NSURLConnection和指针类型错误

时间:2013-11-15 13:51:21

标签: objective-c xml nsurlconnection

这里的问题,我使用的是http请求,但是当我使用NSURLConnection获取我的xml文件并使用该xml文件设置NSString * xml变量时,如果我返回它,我会得到一个块指针错误。但是,如果我采用NSString * xml并将其发送到一个方法,它告诉我什么都没有,并且xml变量设置得很好。另外,如果我在我的方法结束时返回变量,我在父调用方法中得到一个nil。我如何解决这个问题,因为需要返回变量。感谢您的帮助,这是我的代码: 错误:

incompatible block pointer types sending 'NSString *(^)NSURLResponse *, NSData *, NSError *)'to parameter of type 'void(^)(NSURLResponse *,NSData *, NSError *)' 

- (NSString *)restTestRequester: (NSString *)uriRequested serviceUri:(NSString *)sUri parameters:(NSString *)uriParameters technique:(NSString *)requestTechnique  {

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *l_api_key = [prefs stringForKey:@"globalPublicK"];
    NSString *l_secret_key = [prefs stringForKey:@"globalSecretK"];

    l_uri = [NSString stringWithFormat:@"%@/%@", uriRequested, sUri];

    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:l_uri]
                                                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                        timeoutInterval:60.0];

    [theRequest setHTTPMethod:requestTechnique];
    [theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
    [theRequest setValue:@"application/x-www-form-unrlencoded" forHTTPHeaderField: @"Content-Type"];
    [theRequest setValue:l_api_key forHTTPHeaderField: @"EMApikey"];
    [theRequest setValue:[self hmacsha1:l_uri secret:l_secret_key] forHTTPHeaderField: @"EMRequestHash"];
    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if( theConnection )
    {

         [NSURLConnection sendAsynchronousRequest:theRequest
                                           queue:[NSOperationQueue mainQueue]
       incompatible block pointer types error-->   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
         {
     here ---->xml = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
                              autorelease];
         works--->[self myMethod:xml];
         doesn't work---> return(xml);
         }
         ];
    }
    else
    {
        NSLog(@"theConnection is NULL");
    }

    [theConnection release];
    equals nil variable returned-->return xml;
}

2 个答案:

答案 0 :(得分:-1)

我认为问题出在异步调用中,当U trie返回时,U仍然没有任何内容,你的方法在sendAsynchronousRequest完成之前返回。

使用:

NSData* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString* xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

它会等到U得到结果。

答案 1 :(得分:-1)

你有几个主要问题。对于初学者,您的块在连接完成后执行。块连接在 STARTS 之后执行。这意味着您在连接完成之前释放连接。

此外,您正在创建NSURLConnection *theConnection,但从不使用它。当你致电-sendAsynchronousRequest:queue:completionHandler:时,它会创建一个全新的处理程序。

您获得的具体错误是因为您无法从块中返回值。你正在尝试的事实表明你没有完全掌握到底发生了什么。互联网上有很多关于块和异步编程的好参考。我建议你找一个。

NSURL *targetURL = [NSURL URLWithString:l_uri];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:targetUrl                                              
                                                          cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                                       timeoutInterval:60.0];

[theRequest setHTTPMethod:requestTechnique];
[theRequest setValue:@"application/xml" forHTTPHeaderField:@"Accept"];
[theRequest setValue:@"application/x-www-form-unrlencoded" forHTTPHeaderField: @"Content-Type"];
[theRequest setValue:l_api_key forHTTPHeaderField: @"EMApikey"];
[theRequest setValue:[self hmacsha1:l_uri secret:l_secret_key] forHTTPHeaderField: @"EMRequestHash"];
[NSURLConnection sendAsynchronousRequest:theRequest
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
    {
        // Here the connection is complete and the data is usable.
        NSString *xml = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [self myMethod:xml];
        [xml release];
     }];
// Here the Connection has only been queued and no data is available.