2012-08-03 214 views
0

我從iPhone應用程序將數據發送到服務器,但在讀到文章的字符串崩潰的應用程序,不往前走發佈數據到服務器

-(void)submitSurveyAnswers{ 


    NSString*survey_question_response_id="1"; 
    NSString*[email protected]"1"; 

    NSString *question_id [email protected]"1"; 
    NSString *survey_response_answer_id [email protected]"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); 

} 
+0

那麼它在哪裏崩潰? – 2012-08-03 06:37:41

+0

之後survey_response_id,當NSString *後啓動 – user1567956 2012-08-03 06:39:09

回答

1

您已經創建了一個字符串,下面的方法:

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

應該像下面的方式創建:

NSString*[email protected]"1"; //Added '@' while initializing string 
+0

是它現在是固定的,但它沒有輸入數據在服務器 – user1567956 2012-08-03 06:47:12

0
NSString*survey_question_response_id="1"; 

是錯誤的。普通雙引號之間的字符串是C字符串(0終止的字符指針),並且不是有效的NSString實例。我確信你想要1.學會使用調試器2.編寫

NSString *survey_question_response_id = @"1"; 

改爲。