2012-02-08 2 views
0

다른 게시물에서 많이 논의되었지만 불행히도 이러한 스레드가 나에게 도움이되지 않아 게시해야합니다. 나는 connectionDidFinishDownloading 방법 (목록에없는) I에서NSURL 대리인 didReceiveData 메서드가 호출되지 않았습니다.

#pragma mark - 
#pragma mark NSURLConnectionDataDelegate 

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 

    NSLog(@"didReceiveResponse"); 
    [resultData setLength:0]; 

} 


-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    NSLog(@"didReceiveData"); 
[resultData appendData:data]; 


} 

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 

    NSLog(@"didFailWithError"); 

    NSString *errDesc = [NSString stringWithFormat:@"Connection failed: %@", [error description]]; 
    NSLog(@"%@",errDesc); 

} 

-(void) connectionDidFinishLoading:(NSURLConnection *)connection { 

    NSLog(@"didReceiveLoading"); 
} 

아래에 나열된 NSConnectionDataDelegate에 대한 네 가지 방법을 구현는 GET 제 요구에 대한 응답으로 JSON 형식의 일부 데이터를 반환하는 레일 서버에 연결을 시도하고있다 JSON 구문 분석을 수행하지만 didReceiveData 메서드가 호출되지 않으므로 resultData이 비어 있기 때문에 실패합니다.

메시지를 기록하기 때문에 didReceiveResponse 메서드가 호출되었습니다. 서버가 localhost에서 실행 중이므로 요청을 받았음을 확인하고 Charles가 올바르게 응답을 JSON 형식으로 수신했음을 알 수 있습니다. 그래서 서버 측에서 문제가없는 것 같습니다. 그러나 didReceiveData 메서드는 호출되지 않습니다. didFailWithError 메소드도 마찬가지입니다.

누군가 나를 도와 줄 수 있습니까? 내가 뭘 잘못하고 있는지 알 수 없다. NSURL에서 일하는 것은 이번이 처음이므로 많은 감사를드립니다. ARC로 iOS 5에서 작업하고 있습니다.

사티 암

편집 :

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@?%@", kDevelopmentMode ? kLocalHost : kHost, endpoint, parameters]]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

답변

4

내 생각이 앱이 다시 데이터를 가져 오는되지 않는다는 것입니다. 당신의 URL로 이것을 시도해보십시오. 반환 된 원시 데이터와 데이터가 반환되면 jsonserialization을 알 수 있습니다.

NSData *returnedData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://yourURLHere"]]; 

if (returnedData) { 
    NSLog(@"returnedData:%@", returnedData); 

    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:returnedData options:kNilOptions error:Nil]; 
    NSLog(@"jsonDict:%@", jsonDict); 
} 
else 
    NSLog(@"Got Nothing"); 

로그에 원시 데이터와 json 데이터가 표시되면 문제는 NSURLRequest와 관련이 있습니다. 그러나 로그에 "Got Nothing"이 표시되면 얻으려고하는 것에 문제가있는 것입니다.

편집

#import <UIKit/UIKit.h> 

@interface AsyncImage : UIImageView 
{  
    NSMutableData *activeDownload; 
    NSURLConnection *imageConnection; 
} 
@property (nonatomic, retain) NSMutableData *activeDownload; 
@property (nonatomic, retain) NSURLConnection *imageConnection; 
- (void)loadImageFromURL:(NSURL*)url; 
@end 



#import "AsyncImage.h" 

@implementation AsyncImage 
@synthesize activeDownload; 
@synthesize imageConnection; 

- (void)dealloc 
{ 
    [super dealloc]; 
    NSLog(@"dealloc AsyncImage"); 

    [activeDownload release]; 
    [imageConnection cancel]; 
    [imageConnection release]; 
} 

- (void)cancelDownload 
{ 
    [self.imageConnection cancel]; 
    self.imageConnection = nil; 
    self.activeDownload = nil; 
} 


#pragma mark - 
#pragma mark Download support (NSURLConnectionDelegate) 

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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    // Clear the activeDownload property to allow later attempts 
    self.activeDownload = nil; 

    // Release the connection now that it's finished 
    self.imageConnection = nil; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    self.image = [UIImage imageWithData:self.activeDownload]; 

    self.activeDownload = nil; 

    // Release the connection now that it's finished 
    self.imageConnection = nil; 
} 

- (void)loadImageFromURL:(NSURL*)url { 
    if (self.imageConnection!=nil) { [imageConnection release]; } //in case we are downloading a 2nd image 

    self.activeDownload = [NSMutableData data]; 
    // alloc+init and start an NSURLConnection; release on completion/failure 
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest: 
          [NSURLRequest requestWithURL:url] delegate:self]; 
    self.imageConnection = conn; 
    [conn release]; 
} 

@end 
+0

안녕하세요, 나는 당신이 제안한 것을 시도하고 예상대로 원시 데이터와 json 데이터를 로그에 인쇄합니다. 그러나 NSURLRequest의 문제점을 이해하지 못합니다. 어떤 조언이 있습니까? 요청 코드를 편집으로 추가했습니다. 감사! – Satyam

+0

내가 준 코드에서이 URL을 사용했다고 추측합니다. NSURL * url = [NSURL URLWithString : [NSString stringWithFormat : @ "% @ % @? % @", kDevelopmentMode? kLocalHost : kHost, endpoint, parameters]]; 당신이 그랬다면 귀하의 URL과 귀하의 json 데이터가 좋습니다. 나는 편집에 내 코드를 제공 할 것이다. 그게 효과가 있는지보십시오. – Jaybit

+2

내가 잘못하고있는 것을 깨달았다. NSURLConnectionDownloadDelegate의 didFinishDownloading 메서드를 구현 한 것은 모든 데이터를 받았을 때 호출되는 메서드라고 생각했기 때문입니다. 클래스 정의에서 대리자를 제거하고 메서드를 제거하면 모든 것이 의도 한대로 작동합니다. 당신은 왜 didFinishLoading과 다른 방법을 구현하는지 궁금하게 생각합니다. 고맙습니다!! – Satyam

0

.H 파일에 설정 NSURLConnectionDelegate.

관련 문제