我知道这已经在很多其他帖子中讨论了但是我需要发布它,因为那些线程不幸地帮助了我。我正在尝试连接到一个rails服务器,它返回一些JSON格式的数据以响应GET请求。我已经实现了NSConnectionDataDelegate的四种方法,如下所示
#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解析但由于 resultData 为空,因为从未调用过didReceiveData方法而失败。
调用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];
答案 0 :(得分: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
答案 1 :(得分:0)
在.h文件中设置NSURLConnectionDelegate
。