2011-09-01 64 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]; 

}