2011-09-01 98 views
1

我現在有一個問題。我需要將一個transactionID和一個用戶密碼傳遞給一個rest服務,並且假設返回true/false值(以XML格式)。然而,它一直返回我(空)..我完全失去了一些請幫助。iPhone中的其他網絡服務

NSString *urlString = [NSString stringWithFormat:@"https://10.124.128.93:8443/axis2/services/C3WebService/completeWithdrawal Transaction?transactionId=%@&password=%@", _transactionID.text, _userPassword.text]; 

    NSURL *url = [[NSURL alloc] initWithString:urlString]; 
    NSString *result = [[NSString alloc] initWithContentsOfURL:url]; 


    NSLog(@"%@",result); 

我的結果不斷地讓我返回null。我如何繼續從這裏?

+0

[這](http://stackoverflow.com/questions/2005448/how-to -use-nsxmlparser-to-parse-parent-child-elements-that-the-same-name/2005630#2005630)會給你關於解析的適當想法。 –

回答

3

考慮使用NSURLConnection的它可以對結果的回調,也是一個回調以獲取詳細錯誤詳情。它也不會在UI線程上執行(在請求期間不會掛起UI)。

NSURL *url = [NSURL URLWithString:@"http://www.mysite.com"]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 

[request setHTTPMethod:@"GET"]; 
[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

然後你就可以實現委託方法來獲取錯誤,數據和其他細節:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

- (void) connectionDidFinishLoading:(NSURLConnection *)connection { 
    [connection release]; 

    NSString* responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
    NSLog(@"result: %@", responseString); 

    [responseString release]; 
} 

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
     NSLog(@"error - read error object for details"); 
} 
+0

感謝bryanMac ...正是我需要=) –

+0

沒問題。另外,還有其他像RestKit這樣的框架可以處理更多的這個問題。一探究竟。 – bryanmac

+0

嘿Bryanmac ...我得到了「錯誤 - 閱讀錯誤對象的詳細信息」的消息..仍然無法連接...你知道什麼可能是問題嗎? realli非常感謝它.. –

1

您可以嘗試使用「initWithContentsOfURL:encoding:error:」來代替並檢查「錯誤」。此外,使用查爾斯或其他HTTP嗅探器和結果比較直瀏覽器請求(你在瀏覽器中查看結果?)

4
.h: 
    NSMutableData *responseData; 

.m: 
    - (void)load { 
     NSURL *myURL = [NSURL URLWithString:@"https://10.124.128.93:8443/axis2/services/C3WebService/completeWithdrawal Transaction?transactionId=%@&password=%@", _transactionID.text, _userPassword.text]; 
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myURL 
               cachePolicy:NSURLRequestReloadIgnoringLocalCacheData 
              timeoutInterval:60]; 

    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    responseData = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [responseData release]; 
    [connection release]; 
    [textView setString:@"Unable to fetch data"]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"Succeeded! Received %d bytes of data",[responseData 
         `enter code here`        length]); 
    NSString *txt = [[[NSString alloc] initWithData:responseData encoding: NSASCIIStringEncoding] autorelease]; 

}