2012-02-08 46 views
0

我知道這已在很多其他帖子中討論過,但我需要發佈它,因爲這些線程不幸我不幸幫助我。我試圖連接到返回響應GET request.I JSON格式的一些數據的軌服務器已經實現了NSConnectionDataDelegate四種方法如下面列出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"); 
} 

在connectionDidFinishDownloading方法(未上市)我執行JSON解析,但由於從未調用過didReceiveData方法,因此resultData爲空。

didReceiveResponse方法被調用,因爲我看到消息被記錄。服務器正在本地主機上運行,​​所以我可以確認收到請求並使用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

我的猜測是,您的應用程序是沒有得到回數據。試試這個與你的網址,它會讓你知道返回的原始數據,如果數據返回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。但是如果日誌顯示「沒有任何東西」,那麼你想要得到的東西存在問題。

編輯

#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 2012-02-09 11:24:17

+0

我猜你在我給你的代碼中使用了這個URL:NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@「%@%@?%@」,kDevelopmentMode? kLocalHost:kHost,端點,參數]];如果你做到了,那麼你的url和你的json數據是好的。我將在編輯中提供我的代碼。看看是否適合你。 – Jaybit 2012-02-09 16:10:20

+2

我意識到我做錯了什麼。我已經實現了NSURLConnectionDownloadDelegate的didFinishDownloading方法,因爲我認爲這將是在收到所有數據時調用的方法。當我從類定義中刪除委託並刪除方法時,所有方法都按預期工作。你的回答讓我想知道爲什麼我要實施didFinishLoading和其他方法。謝謝!! – Satyam 2012-02-09 17:16:57

0

套裝NSURLConnectionDelegate在.h文件。