建立了一个HTTP请求的类如下
@interface WebConnect : NSObject@property (retain, nonatomic) NSMutableData *returnInfoData;
@property (retain, nonatomic) NSString *resTableInfoData;@property (retain, nonatomic) NSURLConnection *connect_Table_Info;
//////用户信息@property (retain, nonatomic) NSString *userID;
@property bool checkedTableInfoData;
-(void)getTableInformation:(NSString*) nsPostVar:(NSString*)nsUrl:(NSString*)nsReferer:(NSString*)type;
-(int)splitTableInfo:(NSString*)resData;//得到数据.m文件如下-(void)getTableInformation:(NSString*) nsPostVar:(NSString*)nsUrl:(NSString*)nsReferer:(NSString*)type
{
NSData *postData = [nsPostVar dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:nsUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:nsReferer forHTTPHeaderField:@"Referer"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
connect_Table_Info = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //异步处理
}
//收到响应时,会触发
- (void)connection:(NSURLConnection *)aConn didReceiveResponse:(NSURLResponse *)aResponse
{
returnInfoData=[[NSMutableData alloc]init];
}
//每收到一次数据,会调用一次
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data
{
[returnInfoData appendData:data];
}
//网络错误时触发
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError");
}
//全部数据接收完毕时触发
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn
{
NSLog(@"connectionDidFinishLoading11");
if( [aConn isEqual: connect_Table_Info] )
{
resTableInfoData = [[NSString alloc] initWithData:returnInfoData encoding:NSUTF8StringEncoding];
[self splitTableInfo:resTableInfoData];//得到数据以后保存数据到userID中 // [returnInfoData release];
// returnInfoData = nil;
// [resTableInfoData release];
}
}
-(int)splitTableInfo:(NSString*)resData
{
NSArray *array = [resData componentsSeparatedByString:@"^"];
userID=[array objectAtIndex:1];
checkedTableInfoData = true;
return 0;}
然后新建了一个single view application的程序来测试view中建立了一个timer如下-(void)timerResponseTableInfo
{
NSLog(@"开始获取数据");
if (initTableInfo == 0) {
[pwebOperator getTableInformation:参数];//只请求一次数据
initTableInfo = 1;
} if (pwebOperator.checkedTableInfoData == true) {
NSLog(@"%@",pwebOperator.userID);
}
timerGetTableInfo = [NSTimer scheduledTimerWithTimeInterval:(6) target:self selector:@selector(timerResponseTableInfo) userInfo:nil repeats:YES];//得到大厅的信息
}
- (void)viewDidLoad{
pwebOperator=[[WebConnect alloc] init];}
当异步请求到了完整的数据以后checkedTableInfoData 的值会被设定为true
到下一次timer执行的时候NSLog(@"%@",pwebOperator.userID);就报错了,提示pwebOperator.userID为空了但是打印NSLog(@"%@",pwebOperator.resTableInfoData );的值却是存在的,请问大家这是什么原因啊
@interface WebConnect : NSObject@property (retain, nonatomic) NSMutableData *returnInfoData;
@property (retain, nonatomic) NSString *resTableInfoData;@property (retain, nonatomic) NSURLConnection *connect_Table_Info;
//////用户信息@property (retain, nonatomic) NSString *userID;
@property bool checkedTableInfoData;
-(void)getTableInformation:(NSString*) nsPostVar:(NSString*)nsUrl:(NSString*)nsReferer:(NSString*)type;
-(int)splitTableInfo:(NSString*)resData;//得到数据.m文件如下-(void)getTableInformation:(NSString*) nsPostVar:(NSString*)nsUrl:(NSString*)nsReferer:(NSString*)type
{
NSData *postData = [nsPostVar dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:nsUrl]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:nsReferer forHTTPHeaderField:@"Referer"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
connect_Table_Info = [[NSURLConnection alloc] initWithRequest:request delegate:self]; //异步处理
}
//收到响应时,会触发
- (void)connection:(NSURLConnection *)aConn didReceiveResponse:(NSURLResponse *)aResponse
{
returnInfoData=[[NSMutableData alloc]init];
}
//每收到一次数据,会调用一次
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data
{
[returnInfoData appendData:data];
}
//网络错误时触发
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError");
}
//全部数据接收完毕时触发
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn
{
NSLog(@"connectionDidFinishLoading11");
if( [aConn isEqual: connect_Table_Info] )
{
resTableInfoData = [[NSString alloc] initWithData:returnInfoData encoding:NSUTF8StringEncoding];
[self splitTableInfo:resTableInfoData];//得到数据以后保存数据到userID中 // [returnInfoData release];
// returnInfoData = nil;
// [resTableInfoData release];
}
}
-(int)splitTableInfo:(NSString*)resData
{
NSArray *array = [resData componentsSeparatedByString:@"^"];
userID=[array objectAtIndex:1];
checkedTableInfoData = true;
return 0;}
然后新建了一个single view application的程序来测试view中建立了一个timer如下-(void)timerResponseTableInfo
{
NSLog(@"开始获取数据");
if (initTableInfo == 0) {
[pwebOperator getTableInformation:参数];//只请求一次数据
initTableInfo = 1;
} if (pwebOperator.checkedTableInfoData == true) {
NSLog(@"%@",pwebOperator.userID);
}
timerGetTableInfo = [NSTimer scheduledTimerWithTimeInterval:(6) target:self selector:@selector(timerResponseTableInfo) userInfo:nil repeats:YES];//得到大厅的信息
}
- (void)viewDidLoad{
pwebOperator=[[WebConnect alloc] init];}
当异步请求到了完整的数据以后checkedTableInfoData 的值会被设定为true
到下一次timer执行的时候NSLog(@"%@",pwebOperator.userID);就报错了,提示pwebOperator.userID为空了但是打印NSLog(@"%@",pwebOperator.resTableInfoData );的值却是存在的,请问大家这是什么原因啊
- (NSString*)getInfoToServer:(NSString*)nsPostVar:(NSString*)nsUrl:(NSString*)nsReferer
{
NSData *postData = [nsPostVar dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:nsUrl]];
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:nsReferer forHTTPHeaderField:@"Referer"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSMutableData *currenceData=(NSMutableData*)[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//处理返回的数据
NSString *resData = [[NSString alloc] initWithData:currenceData encoding:NSUTF8StringEncoding];
// NSLog(@"%@",resData);
// [resData release];
return resData;
}然后Timer中去调用
if (initTableInfo == 0) {
NSString *resData = [pwebOperator getInfoToServer:参数];//只请求一次数据
[pwebOperator splitTableInfo:resTableInfoData];//得到数据以后保存数据到userID中
initTableInfo = 1;
[resData release];
}
NSLog(@"%@",pwebOperator.userID);//第一次能顺利的打印出来这个值
然后当Timer第二次执行的时候就报错了,说这个值为空,为什么[resData release];第一次执行完毕以后能打印出来,有点搞不清楚,要怎么样才能把得到的值保存啊
是存值的问题
如下
新建了一个类
@interface WebConnect : NSObject
//////用户信息
@property (retain, nonatomic) NSString *userID;-(int)splitTableInfo:(NSString*)resData;
@end@implementation WebConnect
-(int)splitTableInfo:(NSString*)resData
{
NSArray *array = [resData componentsSeparatedByString:@"^"];
userID=[array objectAtIndex:1];
checkedTableInfoData = true;
return 0;}
里面有一个userID在某个地方给这个类声明一个对象,并分配空间
pwebOperator=[[WebConnect alloc] init];
然后将一另外一个字符串赋值给userID如下
NSString *resTimerInfoData = [[NSString alloc] initWithData:XXXXX encoding:NSUTF8StringEncoding];
[pwebOperator splitTableInfo:resTimerInfoData ];
[resTimerInfoData release];然后打印userID的值NSLog(@"%@",pwebOperator .userID);//可以打印出来,没有问题但是我发现我加了一个Timer循环打印这一句就不行了给userID赋值以后马上答应OK,在Timer里面就会报这个userID不存在了
我将@property (retain, nonatomic) NSString *userID;修改成了@property (copy) NSString *userID;我想将userID这个值在类中保存,该怎么做呢,iphone下面还真郁闷这下就比较短了哈,估计照开始那样写,别人都不太好看,那我重新写一下