📜 ⬆️ ⬇️

A simple example of performing an HTTP request to a web service and parsing an XML response

Hello to all habrovchanam!

I just want to say that, although the topic is not “Hello world”, but the task in question is rather simple and is shown here on Habré rather as a means of saving time for developers who may need to work with XML responses from web services. For XML parsing, I used DOMparser (I know about SAXParser, of course), as is usual for me in PHP.

Privatbank (Ukraine) has an API that can be found here . After studying it, it becomes clear that the service is quite informative and it would be convenient to have on your mobile device a means of communicating with it. Let's see how you can survey this service and get such a hackneyed, but sometimes useful information like the current exchange rate. After reading the topic, in principle, it is not difficult to write other useful functions.
')


The service works in a predictable way. You form a request:

privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=en

and in response you receive an XML response of the form:

<account_order> <support/> <version buildNum="2042" buildDate="Thu Nov 29 10:40:45 EET 2007"/> <logged sessioncount="1073" visitscount="298"/> <locale language="ru"> <date id="20090807T17:21:59" traditional="07.08.2009">07  2009, 17:21:59</date> </locale> <request url_base="https://privat24.privatbank.ua/p24/" url="/accountorder" os="Win" win="Y" ie="N"/> <info> <role id="2"/> <dump exchange="" oper="prp" PUREXML="" apicour="" country="ru" /> </info> <exchangerate> <exchangerate ccy="EUR" ccy_name_ru="" ccy_name_ua="Є " ccy_name_en="Euro" base_ccy="RU" buy="450476" unit="1" date="2009-08-26 00:00:00.0"/> <exchangerate ccy="USD" ccy_name_ru=" " ccy_name_ua=" " ccy_name_en="US Dollar" base_ccy="RU" buy="315437" unit="1" date="2009-08-26 00:00:00.0"/> <exchangerate> </account_order> 


Here we are interested in exchangerate nodes with the buy attribute (the service gives the value of the purchase rate in kopecks).

I made the interface easier to work with (TextView + button):



And for the button I wrote the following code:

  public void getCurrencyClick(View v){ TextView tv = (TextView) findViewById(R.id.CurrencyTextView); try{ /*  URL   API,             parse */ URL url = new URL("https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML&apicour&country=ua"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(url.openStream())); doc.getDocumentElement().normalize(); /*       ,   ;     ,   ,          nodeList.getLength */ NodeList nodeList = doc.getElementsByTagName("exchangerate"); Node node = nodeList.item(0); //          NamedNodeMap attributes = node.getFirstChild().getAttributes(); //   buy Node currencyAttribEUR = attributes.getNamedItem("buy"); // ...    String currencyValueEUR = currencyAttribEUR.getNodeValue(); //    ,      Node dateCurrency = attributes.getNamedItem("date"); String dateCurrencyStr = dateCurrency.getNodeValue(); //    tv.setText("   "+dateCurrencyStr+":"+currencyValueEUR+ ""); } catch (Exception e) { tv.setText("   "); }; } 


In my opinion - quite simple. Once again I remind you that this is a test application that I sincerely hope will be useful for someone.

Thanks for attention;)

Source: https://habr.com/ru/post/115054/


All Articles