首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NSURLConnection数据检索

NSURLConnection数据检索
EN

Stack Overflow用户
提问于 2013-09-22 19:09:30
回答 1查看 124关注 0票数 0

我和NSURLConnection有一个非常令人费解的问题。它正在以另一个线程或其他什么方式返回数据,事情并没有按照正确的顺序工作。我应该接收json数据,然后解析它,但是在解析器失败之后,数据实际上是被接收的。这是原木。

2013-09-22 14:44:40.454 WebServiceExample39306:a0b解析错误: Error Domain=NSCocoaErrorDomain Code=3840“操作无法完成(可可错误3840)。”(没有价值。)UserInfo=0xa0b47e0 {NSDebugDescription=No值.} 2013-09-22 14:44:41.312 WebServiceExample39306:a0b成功!接收了112字节的数据

我正在做一个小框架,以方便我的生活连接到rails API,我称之为客观的rails或,但我已经花了整个周末试图找出我做错了什么。下面是代码:

代码语言:javascript
复制
@interface ORObject : NSObject

@property (nonatomic, retain) NSMutableDictionary *properties;
@property (nonatomic, retain) ORWebManager *webManager;
@property (nonatomic, retain) NSString *route;
@property (nonatomic, retain) NSString *singularClassName;

- (id) initWithRoute:(NSString *) route webManager:(ORWebManager *) webManager;
- (id) initWithRoute:(NSString *) route;
- (ORObject *) find:(NSInteger) identifier;
@end

以下是find的实现:

代码语言:javascript
复制
- (ORObject *) find:(NSInteger) identifier
{

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@/%ld.%@", [    ORConfig sharedInstance].baseURL, self.route, (long)identifier, self.webManager.    parser.contentType]];

    ORObject *object = [self.webManager httpGet:url];

    if (object) {
        object.route = self.route;
        object.webManager = self.webManager;
    }

    return object;
}

下面是httpGet类的ORWebManager方法

代码语言:javascript
复制
- (ORObject *) httpGet:(NSURL *)url
{
    ORGetRequester *requester = [[ORGetRequester alloc] init];
    NSMutableData *data = [requester request:url];
    return [self.parser toObject:data];
}

ORGetRequester的超类是ORHTTPRequester,它实现了NSURLConnectionDelegate协议方法。

代码语言:javascript
复制
@interface ORHTTPRequester : NSObject<NSURLConnectionDelegate>
@property (nonatomic, retain) NSMutableData *receivedData;
@property (nonatomic, retain) NSMutableURLRequest *req;
@property (nonatomic, retain) NSURLConnection *connection;
@end

@implementation ORHTTPRequester
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse
    [self.receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection
  didFailWithError:(NSError *)error
{
    NSLog(@"Connection failed! Error - %@ %@",
          [error localizedDescription],
          [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[self.receivedData length]);
}   
@end

最后,这里是ORGetRequester,这就是“魔法”发生的地方。

代码语言:javascript
复制
@interface ORGetRequester : ORHTTPRequester
- (NSMutableData *) request:(NSURL *)url;
@end

@implementation ORGetRequester
- (NSMutableData *) request:(NSURL *)url
{
    self.req = [NSMutableURLRequest requestWithURL:url
                                              cachePolicy:NSURLRequestUseProtocolCachePol    icy
                                          timeoutInterval:60.0];
    self.receivedData = [[NSMutableData alloc] init];
    self.connection = [[NSURLConnection alloc] initWithRequest:self.req delegate:self];

    if (!self.connection) {
        NSLog(@"Connection Failed");
    }

    return self.receivedData;
}
@end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-09-22 20:19:12

代码语言:javascript
复制
self.connection = [[NSURLConnection alloc] initWithRequest:self.req delegate:self];

立即返回,因为它启动异步操作。调用connectionDidFinishLoading:时,需要解析数据。

或考虑使用:

代码语言:javascript
复制
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

当http操作完成时,将调用完成块。处理完成块中的数据,并通知任何需要该数据的对象。如果您不需要其他委托方法,这可能是一个很好的选择。

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

https://stackoverflow.com/questions/18947720

复制
相关文章

相似问题

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