我已經創建了一個新項目,使用它我必須將數據存儲在我的服務器數據庫表上。但每當我發送數據時,它都會在我的表字段中存儲空值。我嘗試了這麼多次,但沒有奏效。如何使用Objective C將數據發佈到Web服務器?
任何幫助,將不勝感激。
PHP腳本代碼
$name = $jsonObj['name'];
$email = $jsonObj['email'];
$phone = $jsonObj['phone'];
$comment = $jsonObj['comment'];
我ViewController.h文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
NSMutableData*mutableData;
}
@property (strong, nonatomic) IBOutlet UITextField *txname;
@property (strong, nonatomic) IBOutlet UITextField *txemail;
@property (strong, nonatomic) IBOutlet UITextField *txcontect;
@property (strong, nonatomic) IBOutlet UITextField *txcomment;
- (IBAction)submit:(id)sender;
@end
我ViewController.m文件
- (IBAction)submit:(id)sender
{
//Here YOUR URL
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my URL…..."]];
//create the Method "GET" or "POST"
[request setHTTPMethod:@"POST"];
//Pass The String to server(YOU SHOULD GIVE YOUR PARAMETERS INSTEAD OF MY PARAMETERS)
NSString *userUpdate =[NSString stringWithFormat:@"name=%@&email=%@&phone=%@& comment=%@&",_txname.text,_txemail.text,_txcontect.text,_txcomment.text,nil];
//Check The Value what we passed
NSLog(@"the data Details is =%@", userUpdate);
//Convert the String to Data
NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding];
//Apply the data to the body
[request setHTTPBody:data1];
//Create the response and Error
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];
//This is for Response
NSLog(@"got response==%@", resSrt);
if(resSrt)
{
NSLog(@"got response");
else
{
NSLog(@"faield to connect");
}
}
你需要充分的PHP腳本代碼,而不是4線 – Shubhank
你的目標C代碼看起來不錯。問題應該來自服務器端。你有反應嗎? – Lion