2011-07-13 160 views
0

我一直在遵循給出的例子: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連接?

+0

此錯誤通常是由您嘗試連接的URL存在問題(錯字,錯誤的字符串格式)造成的。你確定當你NSLog([request.url absoluteString])它返回你期望的值嗎? – xoebus

+0

嘿...你解決了這個問題嗎? 請分享答案:) –

回答

0

首先,你確定它是c#嗎?

其次,你可以嘗試

NSLog(@"error description : %@", [error localizedDescription]); 

這將是更全面

+0

哇,我甚至不知道我是如何將它標記爲C#的?有沒有辦法讓我刪除標籤?此外,它返回的錯誤是:錯誤描述:無法啓動HTTP連接 –

1

您打印出錯誤對象,而不是對象本身的地址。使用這個來代替:

NSLog(@"The error is: %@", error); 

這同樣適用於在responseString:

NSLog(@"%@", responseString); 
0

我知道這是舊的,但它並沒有被標記爲完成呢。我這個糾結了一會兒,它原來我有圓頂是這樣的:

NSString *[email protected]"http://some/url"; NSURL *theUrl = [NSURL URLWithString:[webserviceBase stringByAppendingPathComponent:@"cmd"]];

注意stringByAppendingPathComponent:軋液URL的http://部分。因此,如果您正在做類似的事情,請確保底座包含尾部斜線,並使用stringByAppendingString:代替。

相關問題