1
沒有響應我有下面的代碼,它應該簡單地加載使用POST數據的URL,然後抓住從服務器的HTML響應:從NSURLConnection的
// Send log in request to server
NSString *post = [NSString stringWithFormat:@"username=%@&password=%@", text_field_menu_username.text, text_field_menu_password.text];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request;
[request setURL:[NSURL URLWithString:@"http://example.com/test.php"]]; // Example Only
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
// Handle response
NSString *response_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"Data 1: %@", response);
NSLog(@"Data 2: %@", data);
NSLog(@"Data 3: %@", error);
NSLog(@"Data 4: %@", response_string);
}];
下面是每個NSLog
的輸出:
Data 1: (null)
Data 2: (null)
Data 3: (null)
Data 4:
任何想法我做錯了什麼?
所有變量都是零? – alltom 2013-03-08 02:59:25
我更新了每個'NSLog'的確切輸出的問題 – 2013-03-08 03:01:35
'%d'是用於NSData'length'的錯誤轉換說明符;這是錯誤的簽名,並且在某些體系結構中,大小錯誤。你應該得到這個警告;如果你不是,你應該打開這個警告(「Typecheck調用到'printf' /'scanf'」)。 – 2013-03-08 05:17:09