Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Initially</strong> you have to make an http connection so that you can get the response from your api be it xml response or json response. You can use the following code for it.<br /> Keep the class separate than activity. :-</p> <pre><code>public class Response { String get_url, response; Activity activity; public Response(String url){ this.get_url = url; } public String getResponse(){ InputStream in = null; byte[] data = new byte[1000]; try { URL url = new URL(get_url); URLConnection conn = url.openConnection(); conn.connect(); /* conn.*/ in = conn.getInputStream(); Log.d("Buffer Size +++++++++++++", ""+in.toString().length()); BufferedReader rd = new BufferedReader(new InputStreamReader(in),in.toString().length()); String line; StringBuilder sb = new StringBuilder(); while ((line = rd.readLine()) != null) { sb.append(line); } rd.close(); response = sb.toString(); in.read(data); Log.d("INPUT STREAM PROFILE RESPONSE",response); in.close(); } catch (IOException e1) { Log.d("CONNECTION ERROR", "+++++++++++++++++++++++++++"); // TODO Auto-generated catch block e1.printStackTrace(); } return response; } } </code></pre> <p>You may call the class in your activity like this :-</p> <pre><code>Response res = new Response("your_url"); String getResponse = res.getResponse(); </code></pre> <p>So here you get the response from the api.</p> <p><strong>Now Lets make the parser</strong></p> <pre><code> //Extend the class with Default Handler public class XMLParser extends DefaultHandler { //You must have basic knowledge about Array List and setter/getter methods // This is where the data will be stored ArrayList&lt;Item&gt; itemsList; Item item; String data; String type; private String tempVal; //Create the Constructor public XMLParser(String data){ itemsList = new ArrayList&lt;Item&gt;(); this.data = data; } public byte parse(){ SAXParserFactory spf = null; SAXParser sp = null; InputStream inputStream = null; try { inputStream = new ByteArrayInputStream(data.getBytes()); spf = SAXParserFactory.newInstance(); if (spf != null) { sp = spf.newSAXParser(); sp.parse(inputStream, this); } } /* * Exceptions need to be handled MalformedURLException * ParserConfigurationException IOException SAXException */ catch (Exception e) { System.out.println("Exception: " + e); e.printStackTrace(); } finally { try { if (inputStream != null) inputStream.close(); } catch (Exception e) { } } if (itemsList != null &amp;&amp; itemsList.size() &gt; 0) { // //Log.d("Array List Size",""+tipsList.get(4).getTitle()); return 1; } else { return 0; } } public ArrayList&lt;Item&gt; getItemList(){ return itemsList; } // Here you can check for the xml Tags @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if(localName.equalsIgnoreCase("item")){ item = new Item(); Log.d("Working", "+++++++++++++++++++++++"); } } //tempVal is the variable which stores text temporarily and you // may save the data in arraylists public void characters(char[] ch, int start, int length) throws SAXException { tempVal = new String(ch, start, length); } @Override public void endElement(String uri, String localName, String qName) throws SAXException { if(localName.equalsIgnoreCase("item")){ itemsList.add(item); Log.d("Working in endelement", "+++++++++++++++++++++++"); item.setTitle(tempVal); } } </code></pre> <p><strong>Combining all this :-</strong></p> <p>Now lets see the activity </p> <pre><code> public void oncreate(){ // Do something or mostly the basic code // Call the class to initate the connection and get the data FetchList fl = new FetchList(); fl.execute(); } //Always better to use async task for these purposes public class FetchList extends asyncTask&lt;Void,Void,Byte&gt;{ doinbackground{ // this was explained in first step Response res = new Response("url"); String response = res.getResponse(); XmlParser xml = new XmlParser(response); ArrayList&lt;item&gt; itemList = xml.getItemList(); xml.parse(); } } </code></pre> <p>Well that is all to it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload