php - 将 label.text 发送到 php 文件

标签 php ios objective-c xcode

<分区>

我正在尝试将 label.text 发送到服务器上 php 文件中的变量名“vin”。这是我的代码,如果有问题请帮助调试。谢谢

NSString *post =[[NSString alloc] initWithFormat:@"vin: %@",codeLabel.text];
    NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:NO];

    NSURL *url = [NSURL URLWithString: @"http://url here/test.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:postData];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


    [NSURLConnection sendAsynchronousRequest:request queue:nil completionHandler:nil];

    NSURLResponse *response;
    NSError *error;
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if(error==nil)
        NSLog(@"Error is nil");
    else
        NSLog(@"Error is not nil");
    NSLog(@"success!");

它总是打印“Error is not nil”:(

最佳答案

尝试一下......

Set Delegate:

<NSURLConnectionDataDelegate,NSURLConnectionDelegate>

Declare :

NSMutableData *responceData;
NSURLConnection *connect;

Code:

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

responceData = [[NSMutableData alloc]init];

 NSString *post = [NSString stringWithFormat:@"op=post_comment&business_id=%@&name=%@&email=%@&comment=%@",busId, name.text,email.text,myTextView.text];
 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

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

 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
 [request setURL:[NSURL URLWithString:@"http://www.dignizant.com/red_pages/web_services/index.php"]];
 [request setHTTPMethod:@"POST"];
 [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
 [request setHTTPBody:postData];
 connect = [NSURLConnection connectionWithRequest:request delegate:self];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[responceData appendData:data];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR : %@" , error);

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[NSString stringWithFormat:@"%@",error] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//initialize convert the received data to string with UTF8 encoding
NSString *responceString = [[NSString alloc] initWithData:responceData
                                                 encoding:NSUTF8StringEncoding];
NSLog(@"%@" , responceString);

NSMutableDictionary *message = [responceString JSONValue];
[message valueForKey:@"message"];

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Message" message:[message valueForKey:@"message"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}

希望我有帮助

关于php - 将 label.text 发送到 php 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16417735/

上一篇:ios - MFMailComposeViewController 邮件收件人限制

下一篇:iOS:GUI 自动化测试和持续集成

相关文章:

php - 将字段添加到客户地址并将其显示在管理面板中?

php - 按原样写\r\n

ios - 使用 Swift 取消自动续订订阅

ios - 未知类型名称 "Firebase"

ios - 当多个警报 View 出现在同一个 View Controller 中时,有没有办法错开?

php - 使用 shell 和 ImageMagick 递归调整图像大小

php - html文本区域可以具有只读和可编辑文本的组合吗

ios - 设备方向更改时的自动布局约束问题

objective-c - AFNetworking 并发 Http 请求

ios - 在同一个应用程序上使用两个 Facebook 应用程序 ID 进行测试