Я разрабатываю приложение для iOS 4 для iPad с последним SDK и XCode 4.2.
У меня проблема с веб-службой JSON.Я использую таймер, чтобы проверять каждые 3 секунды, изменились ли некоторые данные.Проблема в том, что данные меняются, но я не вижу этих изменений в моем приложении.
Данные, полученные асинхронно, всегда одинаковы.Это как NSURLConnection
имеет кеш или что-то в этом роде и всегда возвращает первые необходимые данные.
Вот мой код:
ControlPanelJSON.h
#import <Foundation/Foundation.h>
#import "SBJson.h"
#import "WebServiceDelegate.h"
@interface ControlPanelJSON : NSObject
{
SBJsonParser* parser;
NSURLRequest* request;
NSMutableData* receivedData;
BOOL isEncuestaVisible;
BOOL isInfoVisible;
BOOL isTwitterVisible;
id<WebServiceDelegate> delegate;
}
@property (nonatomic, readonly) BOOL isEncuestaVisible;
@property (nonatomic, readonly) BOOL isInfoVisible;
@property (nonatomic, readonly) BOOL isTwitterVisible;
- (id) initWithWebServiceURLString:(NSString*)webServiceURL
delegate:(id<WebServiceDelegate>)del;
- (void)callWebService;
@end
ControlPanelJSON.m
#import "ControlPanelJSON.h"
#define kRootKey @"s"
#define kControlKey @"c"
#define kEstadoKey @"e"
#define kEncuestaTitle @"[ zzz ]"
#define kInfoTitle @"[ yyy ]"
#define kTwitterTitle @"[ xxx ]"
@implementation ControlPanelJSON
@synthesize isEncuestaVisible;
@synthesize isInfoVisible;
@synthesize isTwitterVisible;
- (id) initWithWebServiceURLString:(NSString*)webServiceURL
delegate:(id<WebServiceDelegate>)del
{
if (self = [super init])
{
delegate = [del retain];
request = [[NSURLRequest alloc] initWithURL: [NSURL URLWithString: webServiceURL]
cachePolicy: NSURLCacheStorageNotAllowed
timeoutInterval: 60.0];
parser = [[SBJsonParser alloc] init];
}
return self;
}
- (void) dealloc
{
[parser release];
[request release];
[receivedData release];
[super dealloc];
}
- (void)callWebService
{
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection)
{
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
}
else
{
[delegate errorReceivedWithMessage:NSLocalizedString(@"CONNERROR", nil)];
}
}
#pragma mark -
#pragma mark - NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// Cuando se recibe este mensaje se debe descartar todo lo recibido anteriormente.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection
didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere
[receivedData release];
// inform the user
[delegate errorReceivedWithMessage:
[NSString stringWithFormat:@"Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]]];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSDictionary* datos = [parser objectWithString:json_string error:nil]; // TODO: Otro error que manejar
[connection release];
//[receivedData release];
//[parser release];
[json_string release];
NSArray* data = [datos objectForKey:kRootKey];
for (int i = 0; i < data.count; i++)
{
NSDictionary* object = [data objectAtIndex:i];
NSString* controlName = [object objectForKey:kControlKey];
NSString* controlEstado = [object objectForKey:kEstadoKey];
if ([controlName isEqualToString: kEncuestaTitle])
{
isEncuestaVisible = ([controlEstado isEqualToString: @"1"]);
continue;
}
else if ([controlName isEqualToString: kInfoTitle])
{
isInfoVisible = ([controlEstado isEqualToString: @"1"]);
continue;
}
else if ([controlName isEqualToString: kTwitterTitle])
{
isTwitterVisible = ([controlEstado isEqualToString: @"1"]);
continue;
}
}
[delegate dataReceived];
}
@end
После этой строки кода:
NSString *json_string = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
Я добавилNSLog, и я всегда получаю одно и то же json_string
{"ctrls": [{"control": "[xxx]", "estado": "0"}, {"control": "[yyy] "," estado ":" 0 "}, {" control ":" [zzz] "," estado ":" 0 "}]}
Любая подсказка?