Nsurlconnection 未调用NSURL委托didReceiveData方法
我知道这已经在很多其他帖子中讨论过了,但我需要发布它,因为不幸的是这些帖子并没有帮到我。我正在尝试连接到一个rails服务器,该服务器以JSON格式返回一些数据以响应GET请求Nsurlconnection 未调用NSURL委托didReceiveData方法,nsurlconnection,Nsurlconnection,我知道这已经在很多其他帖子中讨论过了,但我需要发布它,因为不幸的是这些帖子并没有帮到我。我正在尝试连接到一个rails服务器,该服务器以JSON格式返回一些数据以响应GET请求 #pragma mark - #pragma mark NSURLConnectionDataDelegate -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { NSL
#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");
}
在ConnectionDiFinishDownloading方法(未列出)中,我执行JSON解析,但由于从未调用didReceiveData方法,因此该解析失败,因为resultData为空
调用didReceiveResponse方法是因为我看到消息正在被记录。服务器在本地主机上运行,因此我可以确认已收到请求,并且使用Charles,我知道已正确接收JSON格式的响应。因此,服务器端似乎没有问题。但从未调用didReceiveData方法。Didror方法也是如此
有人能帮我吗?我不知道我做错了什么。这是我第一次与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];
我猜你的应用程序无法恢复数据。用你的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数据,那么问题在于NSURL请求。但是如果日志显示“什么都没有”,那么您试图获取的内容就有问题
编辑
#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
#导入
@接口异步映像:UIImageView
{
NSMutableData*动态下载;
NSURLConnection*图像连接;
}
@属性(非原子,保留)NSMutableData*activeDownload;
@属性(非原子,保留)NSURLConnection*imageConnection;
-(void)loadImageFromURL:(NSURL*)url;
@结束
#导入“AsyncImage.h”
@异步映像的实现
@综合动态下载;
@综合图像连接;
-(无效)解除锁定
{
[super dealoc];
NSLog(@“dealoc AsyncImage”);
[动态下载版本];
[图像连接取消];
[图像连接释放];
}
-(作废)取消下载
{
[self.imageConnection取消];
self.imageConnection=nil;
self.activeDownload=nil;
}
#布拉格标记-
#pragma标记下载支持(NSURLConnectionLegate)
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[self.activedata:data];
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
//清除activeDownload属性以允许以后尝试
self.activeDownload=nil;
//现在连接已完成,请释放连接
self.imageConnection=nil;
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
self.image=[UIImage-imageWithData:self.activeDownload];
self.activeDownload=nil;
//现在连接已完成,请释放连接
self.imageConnection=nil;
}
-(void)loadImageFromURL:(NSURL*)url{
如果(self.imageConnection!=nil){[imageConnection release];}//我们正在下载第二个映像
self.activeDownload=[NSMutableData];
//alloc+init并启动NSURLConnection;完成/失败时释放
NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:
[nsurlRequestWithURL:url]委托:自我];
self.imageConnection=conn;
[控制释放];
}
@结束
我猜你的应用程序无法恢复数据。用你的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数据,那么问题在于NSURL请求。但是如果日志显示“什么都没有”,那么您试图获取的内容就有问题
编辑
#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
#导入
@接口异步映像:UIImageView
{
NSMutableData*动态下载;
NSURLConnection*图像连接;
}
@属性(非原子,保留)NSMutableData*activeDownload;
@属性(非原子,保留)NSURLConnection*imageConnection;
-(void)loadImageFromURL:(NSURL*)url;
@结束
#导入“AsyncImage.h”
@异步映像的实现
@综合动态下载;
@综合图像连接;
-(无效)解除锁定
{
[super dealoc];
NSLog(@“dealoc AsyncImage”);
[动态下载版本];
[图像连接取消];
[图像连接释放];
}
-(作废)取消下载
{
[self.imageConnection取消];
self.imageConnection=nil;
self.activeDownload=nil;
}
#布拉格标记-
#pragma标记下载支持(NSURLConnectionLegate)
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
[self.activedata:data];
}
-(无效)连接:(NSURLConnection*)连接失败错误:(NSError*)错误
{
//清除activeDownload属性以允许以后尝试
self.activeDownload=nil;
//现在连接已完成,请释放连接
self.imageConnection=nil;
}
-(无效)连接IDFinishLoading:(NSURLConnection*)连接
{
self.image=[UIImage-imageWithData:self.activeDownload];
self.activeDownload=nil;
//现在连接已完成,请释放连接
self.imageConnection=nil;
}
-(void)loadImageFromURL:(NSURL*)url{
如果(self.imageConnection!=nil){[imageConnection release];}//我们正在下载第二个映像
self.activeDownload=[NSMutableData];
//alloc+init并启动NSURLConnection;完成/失败时释放
NSURLConnection*conn=[[NSURLConnection alloc]initWithRequest:
[nsurlRequestWithURL:url]委托:自我];
self.imageConnection=conn;
[控制释放];
}
@结束
在.h文件中设置NSURLConnectionLegate
。在.h文件中设置NSURLConnectionLegate
。您好,我尝试了您建议的方法,并打印了原始数据和json数据