보관물

Posts Tagged ‘SOAP’

SOAP 형태의 서울시 공공정보 OpenAPI를 iOS에서 사용하기.

모바일 앱 개발을 진행하며, 서울시에서 제공하는 공공정보 API는 매우 유용하게 활용할 수 있어 보였습니다. 그래서, 관련 정보를 연동하기 위한 작업은 진행하는데, 생각보다 바로 되지 않았습니다. 바로, SOAP형태의 웹서비스 연동이 잘 안되었던 것입니다. 그래서, 여기저기 자료를 찾아보니 wsdl2ObjC라는 구글 코드에 있는 오픈 프로젝트도 있었습니다.

wsdl2objC를 간단하게 소개를 하자면, wsdl을 입력해 주면, binding, request 그리고 response에 해당하는 소스를 자동으로 생성해 주는 툴이였습니다. 그러나, 서울시 공공정보 OpenAPI는 그 소스를 이용해도 접근이 안되었습니다.(혹시, 잘 되시는 분들은 알려주시면 좋겠습니다.) 그리고, 몇가지 다양한 오류를 보고 확인하는 작업을 통해 연동 할 수 있었습니다.
지하철 노선의 역 정보를 가져오는 관련 코드는 아래와 같습니다.

NSString *soapMessage =
"<?xml version='1.0' encoding='utf-8'?>"
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>"
"<soapenv:Header>"
"<authInfo:userId xmlns:authInfo='http://mobile.openapi.seoul.go.kr/SeoulOpenAPI/services/SubwayStationService?wsdl'>%@</authInfo:userId>"
"<authInfo:authKey xmlns:authInfo='http://mobile.openapi.seoul.go.kr/SeoulOpenAPI/services/SubwayStationService?wsdl'>%@</authInfo:authKey>"
"</soapenv:Header>"
"<soapenv:Body>"
"<ns5:searchSubwayStationListByLineNum xmlns:ns5='http://service.station.subway.openapi.ubizvalley.com'>"
"<ns5:lineNum>4</ns5:lineNum>"
"<ns5:sortItem />"
"<ns5:sortStd />"
"<ns5:cPage>1</ns5:cPage>"
"<ns5:pageSize>5</ns5:pageSize>"
"</ns5:searchSubwayStationListByLineNum>"
"</soapenv:Body>"
"</soapenv:Envelope>"];

soapMessage = [NSString stringWithFormat:soapMessage, /*개발자 아이디 문자열*/, /*base64로 인코딩된 인증키 값*/]; 

NSURL * url = [NSURL URLWithString:@"http://mobile.openapi.seoul.go.kr/SeoulOpenAPI/services/SubwayStationService?wsdl"]; 

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://service.station.subway.openapi.ubizvalley.com/SubwayStationService/searchSubwayStationListByLineNum" forHTTPHeaderField:@"SOAPAction"]; 
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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

이하는 NSURLConnection 통신의 방식을 따라 수집된 XML을 파싱하여 사용하면 됩니다.
앞으로 서울시 공공정보 OpenAPI를 활용한 다양한 아이폰 앱을 개발해 보아야 겠습니다.

iOS 개발 뉴스 : 8월 10일.

– Invoke에 관련 아티클 : http://blog.jayway.com/2011/08/08/invoke-any-method-on-any-thread/

– iOS개발 환경에서 SOAP 웹서비스를 사용하는 방법을 정리한 아티클 : https://beta-developer.omniture.com/en_US/blog/using-soap-web-services-on-the-iphone

카테고리:iPhone-iPad, Objective-C 태그:, , ,