我一直在遵循給出的例子:http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/76730-webservice-how.html爲了嘗試讓我的iPhone應用程序連接到一個Web服務。然而,當我使用下面的代碼,它將引發我一個錯誤:ASIHttpRequest拋出一個錯誤,我只是不知道爲什麼
-(IBAction) startSend:(id)sender
{
NSURL *url = [NSURL URLWithString:@"mywebsitehere"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
NSString *xmlString = [NSString stringWithString:@""];
xmlString=AS(xmlString,@"<id>");
xmlString=AS(xmlString,@"the id would go here.");
xmlString=AS(xmlString,@"</id>");
xmlString=AS(xmlString,@"<Data>");
xmlString=AS(xmlString,@"Binary Data would be read here.");
xmlString=AS(xmlString, @"</Data>");
NSData* xmlData=[xmlString dataUsingEncoding:NSUTF8StringEncoding];
[request appendPostData:xmlData];
[request setDidFinishSelector:@selector(requestCompleted:)];
[request setDidFailSelector:@selector(requestError:)];
[request setDelegate:self];
[request startAsynchronous];
}
- (void)requestCompleted:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSLog(@"%d", responseString);
}
- (void)requestError:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"error description : %@", [error localizedDescription]);
}
當我運行這一點,我會得到的錯誤信息:錯誤描述:無法啓動HTTP連接。
什麼會導致它無法啓動HTTP連接?
此錯誤通常是由您嘗試連接的URL存在問題(錯字,錯誤的字符串格式)造成的。你確定當你NSLog([request.url absoluteString])它返回你期望的值嗎? – xoebus
嘿...你解決了這個問題嗎? 請分享答案:) –