0

我正在尝试将 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!");

它总是打印“错误不是零” :(

4

1 回答 1

1

试试看....

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];
}

希望我有所帮助

于 2013-05-07T11:36:28.963 回答