当前位置: 代码迷 >> Iphone >> 关于HTTP异步请求的有关问题?了!
  详细解决方案

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

热度:75   发布时间:2016-04-25 06:24:33.0
高分求教关于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{
  相关解决方案