2014-11-01 46 views
1

我發送一個POST請求到服務器,但他們收到它重複相同的日期,甚至時間。這是我的代碼:POST方法接收服務器中重複

-(void) getTerms{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     User *mUser = [[User alloc]init]; 
     NSString *appFile1 = [self getUserFile]; 
     mUser = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile1]; 
     NSString *post = [NSString stringWithFormat:@"Method=GetFAQ&CustomerID=%@",mUser.customerID]; 
     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[ServerName serverName]]]; 


     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setHTTPBody:postData]; 

     NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 
     dispatch_async(dispatch_get_main_queue(), ^{ 
      [SVProgressHUD dismiss]; 
      if(theConnection){ 

       // other codes 

      } 

     }); 

    }); 

} 

我只是想知道我的代碼有什麼問題?在此先感謝

+0

如何被執行的方法是什麼?從通話或按鈕? – 2014-11-02 12:38:04

+0

您使用的是哪個ios版本?我有同樣的問題 – 2014-11-24 10:29:18

+0

即時通訊使用8.1 @LenaBru但我解決了它。讓我發表我的代碼作爲答案:) – Rudi 2014-11-27 15:59:34

回答

0

這是我如何解決了這個問題:

-(void) getCredit{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

     NSString *post = [NSString stringWithFormat:@"Method=%@",mMethod]; 
     NSMutableURLRequest *request = [ServerName postToServer:post]; 

     dispatch_async(dispatch_get_main_queue(), ^{     
      NSURLResponse *requestResponse; 
      NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; 

      NSString *jsonString = [[NSString alloc] initWithBytes:[requestHandler bytes] length:[requestHandler length] encoding:NSUTF8StringEncoding]; 

      NSDictionary *res = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil]; 
      NSDictionary *res1 = [res objectForKey:@"ServerResponse"]; 
     }); 
    }); 
} 
相關問題