将数据从iphone应用程序发布到服务器

时间:2012-08-03 06:35:10

标签: iphone xcode

我正在从iphone应用程序向服务器发送数据,但是当它读取字符串后,它会在应用程序中崩溃并且不会继续前进

-(void)submitSurveyAnswers{


  NSString*survey_question_response_id="1";
  NSString*survey_id=@"1";

  NSString *question_id =@"1";
  NSString *survey_response_answer_id =@"1";
  NSString *post =[[NSString alloc] initWithFormat:@"survey_question_response_id=%@&survey_id=%@&question_id=%@&survey_response_answer_id=%@",survey_question_response_id,survey_id,question_id,survey_response_answer_id];
  NSLog(post);
  NSURL *url=[NSURL URLWithString:@"http://myserver-solutions.com/app/surveyAnswer.php?"];

  NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

 NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];


 NSError *error;
 NSURLResponse *response;
 NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

 NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
 NSLog(@"%@",data);

}

2 个答案:

答案 0 :(得分:1)

您已使用以下方式创建了一个字符串:

NSString*survey_question_response_id="1"; //Missing '@' while initializing string

应该像下面这样创建:

NSString*survey_question_response_id=@"1"; //Added '@' while initializing string

答案 1 :(得分:0)

NSString*survey_question_response_id="1";

错了。普通双引号之间的字符串是C字符串(以0结尾的字符指针),并且不是有效的NSString实例。我确定你想1.学习使用调试器2.写

NSString *survey_question_response_id = @"1";

代替。