我无法从Weather Underground服务接收XML文件

时间:2013-07-20 21:49:06

标签: iphone ios xml api

我正在尝试编写一个使用wunderground api的天气应用程序... 我有一个apiKey,我在下面的代码中使用它,但我在调试区域看不到任何结果... Xcode在一行“EXPRESSION RESULT UNUSED”中向我显示警告......这是问题吗? 请有人帮帮我吗?

#import "WeatherForecast.h"
#import "MainViewController.h"

@implementation WeatherForecast

- (void) queryServiceWithState:(NSString *)state
                andCity:(NSString *)city
                withParent:(UIViewController *)controller {
viewController = (MainViewController *)controller;
responseData = [NSMutableData data];
apiKey = @"c5f79118382c6e91";

NSString *url =
[NSString stringWithFormat:
 @"http://api.wunderground.com/api/%@/conditions/q/%@//%@.xml",
 apiKey, state, city];

theURL = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:theURL];
[[NSURLConnection alloc] initWithRequest:request delegate:self];//EXPRESSION RESULT     UNUSED
}


#pragma mark NSURLConnection Delegate Methods

- (NSURLRequest *)connection:(NSURLConnection *)connection
         willSendRequest:(NSURLRequest *)request
        redirectResponse:(NSURLResponse *)response{
@autoreleasepool {
    theURL = [request URL];
}
return request;
}

- (void)connection:(NSURLConnection *)connection
didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}


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

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Error = %@",error);
}

- (void)connectionDidFinishiLoading: (NSURLConnection *)connection {
NSString *content =
[[NSString alloc]initWithBytes:[responseData bytes]
                        length:[responseData length]
                      encoding:NSUTF8StringEncoding];
NSLog ( @"Data = %@",content);

//...Insert code to parse the content here...

[viewController updateView];

}

@end

我的应用程序还有另外2个.m文件,可能错误在于其中一个

#import "MainViewController.h"
#import "WeatherForecast.h"

@interface MainViewController ()

@end

@implementation MainViewController

- (void)viewDidLoad
{
 [super viewDidLoad];
[self refreshView:self];
}

- (IBAction)refreshView:(id)sender {
 [loadingActivityIndicator startAnimating];
 [self.forecast queryServiceWithState:@"UK" andCity:@"London" withParent:self];
}


- (void)updateView {

//...

[loadingActivityIndicator stopAnimating];
} 

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

#pragma mark - Flipside View

- (void)flipsideViewControllerDidFinish:(FlipsideViewController *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)showInfo:(id)sender
{    
FlipsideViewController *controller = [[FlipsideViewController alloc]               

initWithNibName:@"FlipsideViewController" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:controller animated:YES completion:nil];
}

@end

#import "AppDelegate.h"
#import "WeatherForecast.h"
#import "MainViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    

(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.mainViewController = [[MainViewController alloc]     

initWithNibName:@"MainViewController" bundle:nil];

WeatherForecast *forecast = [[WeatherForecast alloc] init];
self.mainViewController.forecast = forecast;


self.window.rootViewController = self.mainViewController;
[self.window makeKeyAndVisible];
return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{

}

- (void)applicationDidEnterBackground:(UIApplication *)application
{

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{

}

- (void)applicationDidBecomeActive:(UIApplication *)application
{

}

- (void)applicationWillTerminate:(UIApplication *)application
{

}

@end

如果我尝试关闭互联网连接,我可以在调试区域看到“错误信息”,但如果我打开互联网连接,那么我只看到活动指示器永远旋转...

感谢您的回复......我感到迷茫......

1 个答案:

答案 0 :(得分:0)

获取EXPRESSION RESULT UNUSED的位置是连接对象。您通常应将其存储到属性中,以确保在您仍在使用它时不会将其销毁。