我通过单击UIButton执行JSON POST请求,在提交后,可以执行segue执行。因此,提交值后,我再也无法执行POST请求。它显示状态代码200并且响应正常。但是,数据不会反映在后端。这是我的代码:
(IBAction)transitsurveydone:(id)sender {
if([tempArray count]!=0){
/* alert= [[UIAlertView alloc] initWithTitle:@"Survey Submission"
message:@"Please Enter your location"
delegate:self
cancelButtonTitle:@"Modify"
otherButtonTitles:@"Submit",nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
alert.tag=2;
[alert show];*/
NSLog(@"Caption array is %@ %@",captionArray,tempArray);
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"myURL"]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSMutableDictionary *postDict = [[NSMutableDictionary alloc]init];
NSMutableDictionary *d=[[NSMutableDictionary alloc] initWithObjectsAndKeys:@10,@"\"Bus\"", nil];
NSMutableArray *m=[[NSMutableArray alloc] init];
NSString *str1,*str2,*str3,*str4;
// Checking the format
if(tempArray.count==1){
for (int x=0; x<1; x++) {
str1=[NSString stringWithFormat:@"\"%@\"",[captionArray objectAtIndex:x]];
[d setObject:[tempArray objectAtIndex:x] forKey:str1];
}
[request setHTTPBody:[[NSString stringWithFormat: @"{\n \"instance\" : %@,\n \"response_method\": \"web\",\n \"routes\": [\n {%@:%@}\n ]\n}",randomString,str1,[d objectForKey:str1] ]dataUsingEncoding:NSUTF8StringEncoding]];
}
NSLog(@"%@:%@",str1,[d objectForKey:str1]);
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
if (error) {
// Handle error...
return;
}
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSLog(@"Response HTTP Status code: %ld\n", (long)[(NSHTTPURLResponse *)response statusCode]);
NSLog(@"Response HTTP Headers:\n%@\n", [(NSHTTPURLResponse *)response allHeaderFields]);
}
NSString* body = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Response Body:\n%@\n", body);
}];
[task resume];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *respData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"~~~~~ Status code: %d", [response statusCode]);
if([response statusCode]==200){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Transit Survey submitted" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
alert.transform = CGAffineTransformMakeTranslation( 10, 740);
[alert show];
[self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1.0f];
submitteddonthide=NO;
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Transit Survey Submission Failed" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
alert.transform = CGAffineTransformMakeTranslation( 10, 740);
[alert show];
[self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:1.0f];
}
if([prefs integerForKey:@"humandone"]==1){
[self performSegueWithIdentifier:@"transittohuman" sender:nil];
}
else{
[self performSegueWithIdentifier:@"gobackfromtransit" sender:nil];
}
}
`
以上代码在IBAction中
答案 0 :(得分:0)
您的代码看起来很好,没有任何问题。 如果这个问题一直发生,请添加&#34; Advanced Rest Client&#34;附加到Chrome浏览器并测试您的服务器URL传递相应的输入值。如果此流程无法更新后端的值,则后端应该会出现问题。
答案 1 :(得分:0)
有几点想法:
您正在使用状态代码来确定服务器是否正确插入数据。您实际上应该让您的Web服务构建一个肯定的响应(在JSON中,会很棒),说明数据是否已成功插入。您不应仅仅依赖Web服务器的请求响应代码。
我会观察Charles之类的请求,并确保它看起来不错。
您手动构建JSON请求,这非常脆弱。我建议使用Charles来观察请求,并将其复制并粘贴到http://jsonlint.com中,并确保它没问题。
更好的是,使用更健壮,更易于使用的NSJSONSerialization
。
此外,您还要两次发送此请求。丢失第二个请求(不管怎样你不应该做同步请求)并将所有确认逻辑放在会话的完成处理程序块中。
答案 2 :(得分:0)
是。经过一段时间的努力,清理饼干给了我很多帮助: -
这是一大堆代码,非常简单
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [cookieStorage cookiesForURL:[NSURL URLWithString:urlString]];
for (NSHTTPCookie *cookie in cookies) {
NSLog(@"Deleting cookie for domain: %@", [cookie domain]);
[cookieStorage deleteCookie:cookie];
}
感谢reddys先生和Rob先生的建议