Php 对HTTP Post请求排序
我简直疯了。我有两个按顺序排列的http post调用。第一个成功返回。发生这种情况时,将执行第二个调用,但它永远不会返回。我已经检查过了。我将我的php脚本简化为不带参数,只返回字符串的脚本。但它仍然没有回来。我不允许像这样对HTTP请求排序吗?还是我错过了一些愚蠢的事情 我已经尝试了我能想到的一切,但我现在就要开始了。如果我不能解决这个问题,我的应用程序就完蛋了。强调帮助请求 这是第一个电话。这个很好用:Php 对HTTP Post请求排序,php,objective-c,http-post,Php,Objective C,Http Post,我简直疯了。我有两个按顺序排列的http post调用。第一个成功返回。发生这种情况时,将执行第二个调用,但它永远不会返回。我已经检查过了。我将我的php脚本简化为不带参数,只返回字符串的脚本。但它仍然没有回来。我不允许像这样对HTTP请求排序吗?还是我错过了一些愚蠢的事情 我已经尝试了我能想到的一切,但我现在就要开始了。如果我不能解决这个问题,我的应用程序就完蛋了。强调帮助请求 这是第一个电话。这个很好用: NSString *post = [NSString stringWithFormat
NSString *post = [NSString stringWithFormat:@"&id=%@&password=%@",
[[NSUserDefaults standardUserDefaults] objectForKey:@"id"],
passwordTextField.text];
NSString *script = @"verifyPassword.php";
MyDownloader *d = [[MyDownloader alloc] initWithPost:post script:script];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(verificationResultsReceived:)
name:@"connectionFinished"
object:d];
[d.connection start];
这是该调用的通知方法
- (void) verificationResultsReceived: (NSNotification *) n
{
MyDownloader *d = [n object];
[self.activityIndicator stopAnimating];
if ([n userInfo]) {
NSLog(@"information retrieval failed");
} else {
NSArray *response = [NSJSONSerialization JSONObjectWithData:d.receivedData options:kNilOptions error:nil];
if (response) {
if ([[response valueForKey:@"result"] isEqualToString:@"success"]) {
// password is verified - OK to update settings
[self.activityIndicator startAnimating];
NSString *post = @"";
NSString *script = @"test.php";
MyDownloader *d1 = [[MyDownloader alloc] initWithPost:post script:script];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateResultsReceived:)
name:@"connection2Finished"
object:d1];
NSLog(@"I got this far!");
[d1.connection start];
} else {
// incorrect password entered - notify user and halt
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Incorrect Password"
message:@"Invalid password entered. Please try again."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
} else {
NSLog(@"The server has responded with something other than a JSON formatted object");
}
}
[[NSNotificationCenter defaultCenter] removeObserver:self
name: @"connectionFinished"
object:d];
}
这是下一个通知监视器。它永远不会到这里!为什么
- (void) updateResultsReceived: (NSNotification *) n
{
NSLog(@"But why don't I see this?");
[[NSNotificationCenter defaultCenter] removeObserver:self
name: @"connection2Finished"
object:d];
}
如果有帮助,下面是MyDownloader类:
- (id) initWithPost: (NSString *)post
script : (NSString *)script
{
self = [super init];
if (self) {
self->_mutableReceivedData = [NSMutableData new];
//Encode the post string using NSASCIIStringEncoding and also the post string you need to send in NSData format.
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
//You need to send the actual length of your data. Calculate the length of the post string.
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
//Create a Urlrequest with all the properties like HTTP method, http header field with length of the post string.
//Create URLRequest object and initialize it.
self.request = [[NSMutableURLRequest alloc] init];
// make a string with the url
NSString *url = [@"http://www.mySite.com/" stringByAppendingString:script];
// Set the Url for which your going to send the data to that request.
[self.request setURL:[NSURL URLWithString:url]];
//Now, set HTTP method (POST or GET).
[self.request setHTTPMethod:@"POST"];
//Set HTTP header field with length of the post data.
[self.request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//Also set the Encoded value for HTTP header Field.
[self.request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
// Set the HTTPBody of the urlrequest with postData.
[self.request setHTTPBody:postData];
self->_connection =
[[NSURLConnection alloc] initWithRequest:self.request delegate:self startImmediately:NO];
}
return self;
}
- (void) connectionDidFinishLoading:(NSURLConnection *) connection
{
[[NSNotificationCenter defaultCenter]
postNotificationName:@"connectionFinished" object:self];
}
Thanks for any advice.
不确定你在
MyDownloader
类中做了什么,但让我猜猜。您可以创建一个NSURLConnection
实例,并捕获它的connectiondFinishLoading
,在其中发送通知
您必须记住的一点是,当您使用initWithRequest
创建连接时,它实际上已启动
讨论这相当于调用
initWithRequest:delegate:startimediately:
并为startimediately
传递YES
因此,[d.连接启动]
可能没有任何效果,因为连接已经启动。也许在你注册成为观察员之前它已经回来了
顺便说一句,worker类(如downloader)的首选模式是。也许你想看看,一个类总是知道哪些连接是活动的 谢谢。我已经更新了我的帖子,添加了MyDownloader类代码。你说对了一半。我确实创建了一个NSURLConnection,但我没有立即启动它,这就是为什么我要启动该连接。不,我的观点是,
initWithRequest
确实启动了连接。看看房间,好的。请看一下我在上面的编辑中做了什么。在我第一次打电话之前,我登记了那个观察员。这样我就知道,如果我收到了来自第一次呼叫的通知,而不是第二次呼叫的通知,那么这不可能是由于竞争条件造成的。你同意这证明了问题是另外一回事吗?顺便说一句,不是要开始一场离题辩论,但我不同意这种联系已经开始的观点。如果我在第一次http调用中注释掉了行[d.connection start]
,我将完全没有得到响应。为了防止被忽略,我对initWithRequest
的调用包含了NO
的布尔值,用于startimely
参数。您发布的代码没有帮助。您需要显示发出通知的回调。您如何知道要发出哪一个通知?