2013-04-17 36 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