2014-05-07 35 views
0

我想從iOS發送一個字符串數組到PHP。iOS發佈請求到PHP,不同的字符串內容

我可以發送值的陣列,如test, test2 使用以下代碼:

NSError *error = nil; 
     NSData *jsonData = [NSJSONSerialization 
          dataWithJSONObject:formData 
          options:NSJSONWritingPrettyPrinted 
          error:&error]; 

     if ([jsonData length] > 0 && error == nil) 
     { 
      NSData *jsonData = [NSJSONSerialization dataWithJSONObject:formData options:kNilOptions error:&error]; 
      NSString *post = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
      NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
      NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 
      NSLog(@"%@", post); 
      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
      [request setURL:[NSURL URLWithString:@"http://bobbywhite.ca/enterFormData.php"]]; 
      [request setHTTPMethod:@"POST"]; 
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
      [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
      [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
      [request setHTTPBody:postData]; 


      NSURLResponse *response; 
      NSError *error2; 
      NSData *POSTReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error2]; 
      NSString *lastRowSent = [[NSString alloc] initWithData:POSTReply encoding:NSUTF8StringEncoding]; 

      NSLog(@"Sent to PHP"); 
     } 

但是,如果我嘗試發送一個字符串數組,使用字符串如:

INSERT INTO testTable ('field1') VALUES ('value1')

INSERT INTO testTable ('field2') VALUES ('value2')

它不再有效。

回答

0

你有什麼錯誤嗎? 當您嘗試發送這些字符串時,服務器是否收到任何數據?

+0

iOS側沒有錯誤,服務器沒有收到任何數據。 –

+0

和postData有數據? 您是否嘗試使用POST請求來看看發生了什麼? –

+0

我不知道小提琴手是什麼。我會仔細看看的。 –