2012-11-01 21 views
0

我想批量發送到服務器上的數據。 我必須一次發送25個字符串的nsmutablestring數據,然後得到響應,如果響應成功,然後再次發送下一批。 我使用,我使用的HttpConnection class.The代碼如下:發送數據到服務器使用for循環的HTTP POST在iPhone

HttpConnectionClass * ht= [[HttpConnectionClass alloc] init]; 
ht.delegate=self; 
ht.isNetworkIndicator = YES; 
NSString *data = @"" ; 
int i = 0; 
self.dataforserver = [[NSMutableString alloc]init] ; 
[self.dataforserver setString:@""]; 
for (i=0;i<= self.dataArray.count-1; i++) { 
    data =[NSString stringWithFormat:@"A%d%@",[[self.recordIDArray objectAtIndex:i]intValue], [self.dataArray objectAtIndex:i]]; 
    [self.dataforserver appendString:data]; 

    if ((i+1)%25 == 0 && i!=0) { 

       [ht makeHttpConnection:[NSString stringWithFormat:@"http://www.url.com?action=%d&subscriberid=%d&type=%@",2,56904,@"full"] withData:self.dataforserver]; 

     NSLog(@"in for loop dataforserver is %@",dataforserver); 

     [dataforserver setString:@""];  
    } 
     } 
    if (dataforserver != @"") { 
    NSLog(@"not in for loop dataforserver is %@",dataforserver); 
    [ht makeHttpConnection:[NSString stringWithFormat:@"http://www.url.com?action=%d&subscriberid=%d&type=%@",2,56904,@"full"] withData:self.dataforserver]; 
    } 

我得到下面的方法迴應

-(void)getHttpData:(NSObject*)data isError:(BOOL)isErr errorMsg:(NSString*)err{ 

    NSString *response=(NSString*)data; 
    NSLog(@"response is %@",response); 


    } 

我想繼續循環只有當我得到的迴應「成功」。 任何人都可以建議如何實現它。 我很新iOS編程。請幫助

回答

0

我解決了這個問題,通過使用ASIHttpRequest類在github上可用。 我張貼,我使用的代碼,如果有人得到了同樣的問題

{ 
NSString *data = @"" ; 
int i = 0; 
int batchCount =5; 
NSString *type = @"full"; 

self.dataforserver = [[NSMutableString alloc]init] ; 
[self.dataforserver setString:@""]; 

for (i=0;i< self.dataArray.count; i++) { 

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.myurl.com/gprsin?action=%d&//subscriberid=%d&type=%@",2,111,type]]; 

    data =[NSString stringWithFormat:@"A%d%@",[[self.recordIDArray objectAtIndex:i]intValue], [self.dataArray objectAtIndex:i]]; 
    [self.dataforserver appendString:data]; 

    if ((i+1)%batchCount == 0 && i!=0) { 
     NSString *response = @""; 
     int j = 0; 

     while (response == @"" && j<3) { 
     //Using while loop is not necessary,I am using this because it was required to try three times till getting response.  
      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
      [request setPostBody:[[NSMutableData alloc] initWithData:[dataforserver dataUsingEncoding:NSUTF8StringEncoding]]]; 
      [request setRequestMethod:@"POST"];     
      [request startSynchronous]; 
      NSError *error = [request error]; 
      if (!error) { 
       response = [request responseString]; 
       NSLog(@"response is %@",response);      
       [dataforserver setString:@""]; 
      } 
      else 
      { 
       // unable to connect show alert 
       NSLog(@"there is some error coming and the error is:%@",error); 
       return; 
      } 


      if ([response isEqualToString:@"SUCCESS"]) { 


       for (int k = i-batchCount+1; k<=i; k++) { 

        [self storeContactInDB:[self.dataArray objectAtIndex:k]:[[self.recordIDArray objectAtIndex:k]intValue]];//if you want to store contacts in database along only after successfully sending it to server 

       } 

       type = @"INCREMENT"; 

      } 
      else if ([response isEqualToString:@"FAIL"]) 
      { 
       //show alert 
       return; 
      } 
      j++; 
     } 
    } 
    if (dataforserver != @"" && i== self.dataArray.count-(self.dataArray.count%batchCount) ) 
    { 
     NSString *response = @""; 
     int j = 0; 

     while (response == @"" && j<3) { 

      ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
      [request setPostBody:[[NSMutableData alloc] initWithData:[dataforserver dataUsingEncoding:NSUTF8StringEncoding]]]; 
      [request setRequestMethod:@"POST"]; 
      [request startSynchronous]; 
      NSError *error = [request error]; 
      if (!error) { 
       response = [request responseString]; 
       NSLog(@"response is %@",response); 
      } 

      else 
      { 
       // unable to connect show alert 
       NSLog(@"there is some error coming and the error is:%@",error); 
       return; 
      } 
      if ([response isEqualToString:@"SUCCESS"]) { 

       for (i = self.dataArray.count-(self.dataArray.count%batchCount); i<self.dataArray.count; i++) 
       { 

        [self storeContactInDB:[self.dataArray objectAtIndex:i]:[[self.recordIDArray objectAtIndex:i]intValue]]; 

       } 

       type = @"INCREMENT"; 
      } 
      else if ([response isEqualToString:@"FAIL"]) 
      { 
       //show alert 
       return; 
      } 
      j++; 
     } 
    } 
} 

}

相關問題