未调用 NSURL 委托 didReceiveData 方法 [英] NSURL delegate didReceiveData method not called

查看:122
本文介绍了未调用 NSURL 委托 didReceiveData 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这已经在很多其他帖子中讨论过,但我需要发布它,因为不幸的是这些帖子对我没有帮助.我正在尝试连接到一个 Rails 服务器,该服务器以 JSON 格式返回一些数据以响应 GET 请求.我已经为 NSConnectionDataDelegate 实现了下面列出的四种方法

I know this has been discussed in a lot of other posts but I need to post it because those threads have not helped me unfortunately. I am trying to connect to a rails server which returns some data in JSON format in response to a GET request.I have implemented the four methods for NSConnectionDataDelegate as listed below

#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 为空,因此失败.

In the connectionDidFinishDownloading method (not listed) I carry out the JSON parsing but that fails since resultData is empty since didReceiveData method is never called.

调用 didReceiveResponse 方法是因为我看到正在记录的消息.服务器在 localhost 上运行,因此我可以确认收到了请求,并使用 Charles 我知道以 JSON 格式正确接收了响应.所以在服务器端似乎没有问题.但是 didReceiveData 方法从未被调用.didFailWithError 方法也不是.

The didReceiveResponse method is called because I see the message being logged. The server is running on localhost so I can confirm the request is received and using Charles I know the response is correctly received in JSON format. So there seems to be no problem on the server side. But the didReceiveData method is never called. Neither is didFailWithError method.

有人可以帮我解决这个问题吗?我无法弄清楚我做错了什么.这是我第一次使用 NSURL,因此将不胜感激.我正在使用 ARC 开发 iOS 5.

Can someone please help me with this? I cannot figure out what I am doing wrong. This is the first time I am working with NSURL so it would be much appreciated. I am working on iOS 5 with ARC.

萨蒂扬

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

推荐答案

我猜你的应用没有取回数据.用你的 url 试试这个,它会让你知道返回的原始数据,如果返回数据,它会让你知道 jsonserialization.

My guess would be that your app is not getting data back. Try this with your url it will let you know the raw data returned, and the jsonserialization if data is returned.

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 上.但是,如果日志显示一无所有",那么您尝试获取的内容就有问题.

if your log displays the raw data and the json data then The issue is with your NSURLRequest. But if the log displays "Got Nothing" then there is a problem with what your trying to get.

编辑

#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

这篇关于未调用 NSURL 委托 didReceiveData 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆