如何使用iOS中的Web服务通过url将数据发布到数据库

时间:2014-02-12 11:42:58

标签: ios iphone objective-c

我有3个文本字段,我想通过网址将用户输入的数据发布到数据库中。 我有连接到数据库的url,但在下面的代码中。 连接成功,但是当我在三个UITextfields(名称,年龄,工资),值中发布数据时 存储在数据库中显示为null。 我想检查在数据库中的textFields中输入的值。 请问任何人告诉我在我的代码中应该做些什么更改来发布数据。

·H

#import

@interface ViewController : UIViewController
{






    __weak IBOutlet UITextField *age;

    __weak IBOutlet UITextField *Name;

    __weak IBOutlet UITextField *Salary;
}
@property (weak, nonatomic) IBOutlet UITextField *age;
@property (weak, nonatomic) IBOutlet UITextField *Name;
@property (weak, nonatomic) IBOutlet UITextField *Salary;



- (IBAction)send:(id)sender;




@end

.M

#import "ViewController.h"

@interface ViewController ()
{


    NSMutableData *recievedData;
NSMutableData *webData;
    NSURLConnection *connection;
    NSMutableArray *array;
    NSMutableString *first;


}
@end

@implementation ViewController
@synthesize age;
@synthesize Name;
@synthesize Salary;

- (void)viewDidLoad
{




    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)send:(id)sender {
    NSString *post =[NSString stringWithFormat:@"?&username=%@&password=%@&emailid=%@",
                     age.text, Name.text,Salary.text];

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

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

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

    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.3.170:8090/RestWebService/rest/person"]]];
    [request setHTTPMethod:@"POST"];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];

    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];

    [request setHTTPBody:postData];


    NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(conn)
    {      webData=[NSMutableData data];
        NSLog(@"Connection successfull");
        NSLog(@"GOOD Day My data %@",webData);
    }
    else
    {
        NSLog(@"connection could not be made");

    }
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [webData setLength:0];
    NSLog(@"DidReceiveResponse");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [webData appendData:data];
    NSLog(@"DidReceiveData");
    NSLog(@"DATA %@",data);
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Error is");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[webData length]);
    NSLog(@"Data is %@",webData);



    // NSLog(@"receivedData%@",_receivedData);

    NSString *responseText = [[NSString alloc] initWithData:webData encoding: NSASCIIStringEncoding];
    NSLog(@"Response: %@", responseText);//holds textfield entered value

    NSLog(@"");

    NSString *newLineStr = @"\n";
    responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];

    NSLog(@"ResponesText %@",responseText);


}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

1 个答案:

答案 0 :(得分:0)

带教程

的示例应用程序

connecting-to-php-service-from-ios

借助NSURLConnection委托方法处理结果数据

  NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",@"Raja",@"deepi"];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]];


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

    NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self];

    if( theConnection ){
        // indicator.hidden = NO;
        mutableData = [[NSMutableData alloc]init];
    }

您的PHP代码

<?php
    $username = $_POST['username'];
    $password = $_POST['password'];

//check your condition 



    echo $result;
?>