gpt4 book ai didi

ios - NSURLConnection 数据检索

转载 作者:行者123 更新时间:2023-11-29 03:37:35 24 4
gpt4 key购买 nike

我在使用 NSURLConnection 时遇到了一个非常令人费解的问题。它在另一个线程或其他东西中返回数据,事情没有按正确的顺序工作。我应该接收 json 数据然后解析它,但实际上是在解析器失败后才接收到数据。这是日志。

2013-09-22 14:44:40.454 WebServiceExample[39306:a0b] Parsing Error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No value.) UserInfo=0xa0b47e0 {NSDebugDescription=No value.} 2013-09-22 14:44:41.312 WebServiceExample[39306:a0b] Succeeded! Received 112 bytes of data

我正在制作一个小框架来简化我连接到 Rails API 的生活,我称它为 objective rails OR,但我已经花了整个周末试图找出我做错了什么。这是代码:

@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

下面是查找的实现:

- (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;
}

这是 ORWebManager 类的 httpGet 方法

- (ORObject *) httpGet:(NSURL *)url
{
ORGetRequester *requester = [[ORGetRequester alloc] init];
NSMutableData *data = [requester request:url];
return [self.parser toObject:data];
}

ORGetRequester 的父类(super class)是 ORHTTPRequester,它实现了 NSURLConnectionDelegate 协议(protocol)方法

@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,这就是“魔法”发生的地方。

@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

最佳答案

self.connection = [[NSURLConnection alloc] initWithRequest:self.req delegate:self];

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

或者考虑使用:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

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

关于ios - NSURLConnection 数据检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18947720/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com