2013-04-17 87 views
0

我正面臨一個大問題,HTTP Post方法php和ios

我想發送數據到我的服務器,我的數據是一個字符串。

我不希望使用GET方法,因爲該字符串可能會很長,所以我想使用POST方法,但一切順利,錯了,如果有人能幫助我,他會是我心目中的英雄:)

這是我的PHP代碼:

<?php include("config.inc.php"); 
if (isset($_POST['contentInterro']) && $_POST['contentInterro'] !="") { 
//$id_user = $_POST['contentInterro']; 
//$db->sql_query("INSERT INTO interrogations VALUES(DEFAULT, '$id_user')"); 
echo "succes"; 
}else{ 
echo "This is an error"; 
} 

?> 

這裏是我的應用程序代碼:

NSData *postData = [stringToPost2 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 


    NSMutableURLRequest *requestPost = [[NSMutableURLRequest alloc] init]; 

    NSURL *urlPost = [NSURL URLWithString:@"http://buzznapps.fr/FDF/postInterrogation.php"]; 

    [requestPost setURL:urlPost]; 
    [requestPost setHTTPMethod:@"POST"]; 
    [requestPost setValue:@"lol" forHTTPHeaderField:@"contentInterro"]; 



    NSError *errorURL; 
    NSURLResponse *response; 
    NSData *urlData = [NSURLConnection sendSynchronousRequest:requestPost returningResponse:&response error:&errorURL]; 

    NSString *str = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding]; 

    NSLog(@"Str = %@",str); 

我總是得到一個錯誤,我已經在網絡上搜索,我不知道如何得到這個數據! 感謝您的幫助。

回答

1
#define TIMEOUT_INTERVAL 60 
    #define CONTENT_TYPE @"Content-Type" 
    #define URL_ENCODED @"application/x-www-form-urlencoded" 
    #define GET @"GET" 
    #define POST @"POST" 

    -(NSMutableURLRequest*)getNSMutableURLRequestUsingGetMethodWithUrl:(NSString*)url 
     { 
      NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL]; 
      [req setHTTPMethod:GET]; 
      return req; 
     } 

     -(NSMutableURLRequest*)getNSMutableURLRequestUsingPOSTMethodWithUrl:(NSString *)url postData:(NSString*)_postData 
     { 
      NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:TIMEOUT_INTERVAL]; 
      [req setHTTPMethod:POST]; 
      [req addValue:URL_ENCODED forHTTPHeaderField:CONTENT_TYPE]; 
      [req setHTTPBody: [_postData dataUsingEncoding:NSUTF8StringEncoding]]; 
      return req; 
     } 

@try 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
    NSString *_postData = [NSString stringWithFormat:@"user_name=%@&password=%@",@"user_name",@"password"]; 
    NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:_url postData:_postData]; 
    [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
    { 
     if (error) 
     { 
      [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
      NSLog(@"error==%@==",[error localizedDescription]); 
     } 
     else 
     { 
      NSError *errorInJsonParsing; 
      NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&errorInJsonParsing]; 

      if(errorInJsonParsing) //error parsing in json 
      { 
       [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
       NSLog(@"error in json==%@==",[error localizedDescription]); 
      } 
      else 
      { 
       //do some operations 
      } 
     } 
    }]; 
} 
@catch(NSException *exception) 
{ 
    NSLog(@"error in exception==%@==",[exception description]); 
} 



same way it works for the get method, just call the 

NSMutableURLRequest *req = [self getNSMutableURLRequestUsingGetMethodWithUrl:_url];代替NSMutableURLRequest *req = [self getNSMutableURLRequestUsingPOSTMethodWithUrl:_url postData:_postData];

+1

太謝謝你了! – user2252092

+0

你如何使用@try?我已經將這段代碼粘貼到一個NSObject類中,除了在程序中給出一個意外錯誤的@try行之外,一切都很好。 @ try @ catch必須進入方法嗎? – marciokoko

+0

@marciokoko:是的 – Pradeep