Calling a Service and create DOMDocument from Response with AppEngines URLFetch
This is rather simple task and well documented is the part on doing the call. Though in my case the response is a XML and I want it as a DOMDocument to do my thing. And here is the short snippet for a method that does the call and populates the DOMDocument:
private Document domDocument; private void doCall(String serviceUrl){ try { URL url = new URL(serviceUrl); try { DocumentBuilder parser = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder(); domDocument = parser.parse(new InputSource(new InputStreamReader(url.openStream(),"UTF-8"))); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (MalformedURLException e) { System.out.println(e); //TODO do something about exceptions } catch (IOException e) { System.out.println(e); } }I use this private method in some other methods of my service to do the different calls to a service (search, look for a specific item etc.). After the call I do work on the populated domDocument and create the objects. What’s missing yet is some generic error checking built into the doCall method. But that’s something I do later on.