2

私は何かおかしなことに悩まされています。以前ASIHTTPRequestはWebサービスからデータを受信して​​いましたが、すべて正常に機能していました。を使用するように切り替えましたがNSURLConnection、同じデータを受け取り、同じ方法で解析していますが、コードが。を使用してデータを認識しませんNSURLConnection

これが私が受け取っているデータです(からNSLog

Did receive data: {"d":"[{\"id\":1.0,\"Category\":1,\"hPlan\":0.0,\"Tip\":\"It takes 3500   
calories to gain a pound. If you want to lose a pound per week, reduce your calorie
intake by 250 calories and incorporate daily physical activity that will burn 250   
calories.\",\"TipDate\":\"2012-05-12T00:00:00\",\"TimeStamp\":\"AAAAAAAAB9I=\"}]"}


2012-06-06 09:42:11.809 StaticTable[27488:f803] Jsson Array: 0  
2012-06-06 09:42:11.809 StaticTable[27488:f803] Jsson Array: (null)

コード:

#import "UYLFirstViewController.h"
#import "MBProgressHUD.h" 
#import "JSON.h"

@interface UYLFirstViewController ()

@end

@implementation UYLFirstViewController

#pragma mark -
#pragma mark === UIViewController ===
#pragma mark -

@synthesize MessageField;
@synthesize jsonArray = _jsonArray;
@synthesize TipLabelField;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    self.title = NSLocalizedString(@"Tickle!", @"Tickle!");
     self.tabBarItem.image = [UIImage imageNamed:@"heart_plus"];

    [self GetTipOfDay];

}
return self;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
-(BOOL)GetTipOfDay{

NSDate *date = [NSDate date];

NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];


NSString *yourOriginalString = @"Tip of the Day for ";

yourOriginalString = [yourOriginalString stringByAppendingString:dateString];
TipLabelField.text = yourOriginalString;


NSURL *url = [NSURL URLWithString:@"http://www.mysite.com/api/GetHealth.asmx/getTipOfDay"];


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

[NSURLConnection connectionWithRequest:request delegate:self];



// Clear text field
MessageField.text = @"";

// Start hud
  MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  hud.labelText = @"Gathering Tip of the Day...";

return TRUE;

}

- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{

[MBProgressHUD hideHUDForView:self.view animated:YES];


NSLog(@"Did receive data: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);


NSDictionary *responseDict = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] JSONValue];

NSString *jsonResponse = [responseDict objectForKey:@"d"];

self.jsonArray = [jsonResponse JSONValue];


NSLog(@"Jsson Array: %d", [jsonArray count]);
NSLog(@"Jsson Array: %@", jsonArray);


NSEnumerator *myEnumerator;
myEnumerator = [jsonArray objectEnumerator];
int i;
i=0;
id myObject;

while (myObject = [myEnumerator nextObject])
{
    NSDictionary *itemAtIndex = (NSDictionary *)[self.jsonArray objectAtIndex:i];

    NSLog(@"Checking for games");

    NSString *myCheck = [itemAtIndex objectForKey:@"FName"];

    if ([myCheck length] != 0)
    {
        // NSLog(myCheck);
        MessageField.text = myCheck;
    }
}

} 



- (void)viewDidUnload {
[self setMessageField:nil];
[self setTipLabelField:nil];
[super viewDidUnload];
}
@end


#import <UIKit/UIKit.h>

@interface UYLFirstViewController : UIViewController{
  NSMutableArray *jsonArray;  
}
@property (weak, nonatomic) IBOutlet UILabel *MessageField;
@property (weak, nonatomic) NSMutableArray *jsonArray;
@property (weak, nonatomic) IBOutlet UILabel *TipLabelField;

-(BOOL)GetTipOfDay;


@end
4

4 に答える 4

3

-didRecieveDataバイトとチャンクが入ってくるときに複数回呼び出すことができます。ロジックをに移動する必要があります-connectionDidFinishLoading。これにより、接続が完全に完了し、データを解析する準備ができたときに通知されます。

于 2012-06-06T14:35:12.367 に答える
0

この問題は、Web サービスからのフェッチとは何の関係もないようです。配列を __strong として定義する必要がありました。すべての助けをありがとう。物事をより良くする方法について、いくつかの良いアイデアを得ることができました。

于 2012-06-07T13:21:47.313 に答える