【发布时间】:2015-04-09 11:58:48
【问题描述】:
我通过单击 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中
【问题讨论】:
-
您可能想检查后端发生了什么。是否接收连接?如果它返回 200,它对数据做了什么?
-
它包含字母数字格式的数据。但是,后端没有保存数据
标签: ios objective-c nsurlrequest nsurlsession nsmutableurlrequest