2011-12-09 21 views
0

我是新的iPhone應用程序開發得到ASMX網頁serivce訪問, 可以在任何一個告訴我,我如何使用數據,並使用iPhone應用程序訪問ASMX web服務中獲取數據。 請緊急。 在此先感謝。如何發佈和使用iPhone應用程序

回答

1

我認爲ASMX web服務的SOAP Web服務,你應該在這裏閱讀我的博客文章 -

http://www.makebetterthings.com/iphone/call-soap-web-service-from-iphone/

首先調用一個SOAP服務,我創建SOAP請求字符串如下。

NSString *soapMessage = @"<?xml version="1.0" encoding="utf-8"?>n" 
     "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n" 
     "<soap:Body>n" 
     "<CelsiusToFahrenheit xmlns="http://tempuri.org/">n" 
     "<Celsius>50</Celsius>n" 
     "</CelsiusToFahrenheit>n" 
     "</soap:Body>n" 
     "</soap:Envelope>n"; 


After creating the SOAP request I create a NSMutableRequest to send this request to server. 

NSURL *url = [NSURL URLWithString:@"http://w3schools.com/webservices/tempconvert.asmx"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[theRequest addValue: @"http://tempuri.org/CelsiusToFahrenheit" forHTTPHeaderField:@"SOAPAction"]; 
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 

if(theConnection) 
{ 
    webData = [[NSMutableData data] retain]; 
} 
else 
{ 
    NSLog(@"theConnection is NULL"); 
} 

在發出請求之後,我們可以在NSURLConnection的委託方法中收集XML響應。

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webData setLength: 0]; 
} 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webData appendData:data]; 
} 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"ERROR with theConenction"); 
    [connection release]; 
    [webData release]; 
} 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSLog(@"DONE. Received Bytes: %d", [webData length]); 
    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@",theXML); 
    [theXML release]; 
} 

統計在theXML字符串XML響應後 - (空)connectionDidFinishLoading:(NSURLConnection的*)連接 我們可以使用解析爲TBXML你喜歡的任何其他XML解析器這個字符串。

+0

我加入這個代碼,並能夠太運行它,但我也越來越陣列作爲返回PARAM,我需要捕捉數組列表。 – Selwyn

+0

你可以請在這裏回覆? – Saurabh

+0

我不知道你是如何獲得Web服務響應中的數組?你在XML格式? – Saurabh

相關問題