读书人

关于HTTP异步请求的有关问题?了!

发布时间: 2012-08-30 09:55:54 作者: rapoo

高分求教关于HTTP异步请求的问题?请教各位了!!!!!!!!!!!!!
建立了一个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 );的值却是存在的,请问大家这是什么原因啊



[解决办法]
为什么不用asihttprequest呢?你这个太多代码了,有空我再来看
[解决办法]
跟另一个帖子一样的问题:userID=[array objectAtIndex:1]; -> self.userID=[array objectAtIndex:1];

读书人网 >Iphone

热点推荐