每当我尝试将某些内容发布到我的 PHP 服务器时,我都会收到以下消息。似乎代码正在连接到服务器,但没有返回任何数据,并且发布数据没有通过。它通过我制作的 Java 应用程序运行,所以我可以保证他们的 PHP 没有问题。如果您可以帮助我,或者需要更多代码来帮助我,请提出要求。谢谢。
这是为 NSURLConnection 准备变量的代码:
NSString *phash = [NSString stringWithFormat:@"%d",phashnum];
[phash stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
name = _nameField.text;
[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
email = _emailField.text;
[email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
这是我的 NSURLConnection 的代码:
NSString *urlPath = [NSString stringWithFormat:@"http://54.221.224.251"];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *stringdata = [NSString stringWithFormat:@"name=%@&email=%@&phash=%@",name,email,phash];
NSOperationQueue *queue= [[NSOperationQueue alloc]init];
NSString *postData = [[NSString alloc] initWithString:stringdata];
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if ([data length] > 0 && connectionError==nil){
NSLog(@"Connection Success. Data Returned");
NSLog(@"Data = %@",data);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
else if([data length] == 0 && connectionError == nil){
NSLog(@"Connection Success. No Data returned.");
NSLog(@"Connection Success. Data Returned");
NSLog(@"Data = %@",data);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
else if(connectionError != nil && connectionError.code == NSURLErrorTimedOut){
NSLog(@"Connection Failed. Timed Out");
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
else if(connectionError != nil)
{
NSLog(@"%@",connectionError);
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
int code = [httpResponse statusCode];
NSString *coder = [NSString stringWithFormat:@"%d",code];
NSLog(@"%@",coder);
}
}];
提前致谢。