![](/img/trans.png)
[英]Laravel api http requests sequencing / queued on 2 clients with different browsers
[英]Sequencing HTTP Post requests
我絕對會失去理智。 我有兩個已排序的http帖子調用。 第一個成功返回。 發生這種情況時,將執行第二個調用,但永遠不會返回。 我檢查了一切。 我將我的php腳本簡化為不帶參數的地方,只返回一個字符串。 但是它仍然不會回來。 我是否可以像這樣對HTTP請求進行排序? 還是我只是想念一些愚蠢的東西?
我已經嘗試了所有我能想到的東西,但是現在變得越來越。 如果無法解決此問題,則我的應用程序已死在水中。 強調幫助請求!!!
這是第一個電話。 這個很好用:
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
實例,並在發送通知的地方捕獲其connectionDidFinishLoading
。
您必須記住的一件事是,當您使用initWithRequest
創建連接時,它實際上已啟動。
討論區
這等效於調用initWithRequest:delegate:startImmediately:
並為startImmediately
傳遞YES
。
因此, [d.connection start];
由於連接已經開始,因此可能不會起作用。 也許在您注冊為觀察者之前它已經回來了!
順便說一句,對於工作者類(例如下載器)而言,首選模式是Singleton Pattern 。 也許您想看看一個類總是知道哪些連接處於活動狀態。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.