我和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或,但我已经花了整个周末试图找出我做错了什么。下面是代码:
@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的实现:
- (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方法
- (ORObject *) httpGet:(NSURL *)url
{
ORGetRequester *requester = [[ORGetRequester alloc] init];
NSMutableData *data = [requester request:url];
return [self.parser toObject:data];
}ORGetRequester的超类是ORHTTPRequester,它实现了NSURLConnectionDelegate协议方法。
@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发布于 2013-09-22 20:19:12
self.connection = [[NSURLConnection alloc] initWithRequest:self.req delegate:self];立即返回,因为它启动异步操作。调用connectionDidFinishLoading:时,需要解析数据。
或考虑使用:
+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler当http操作完成时,将调用完成块。处理完成块中的数据,并通知任何需要该数据的对象。如果您不需要其他委托方法,这可能是一个很好的选择。
https://stackoverflow.com/questions/18947720
复制相似问题