Search code examples
javaandroideclipseweb-serviceswebservice-client

Android webservice client: How data extract data from xml file


I'm creating an android webservice client(restful web service). And im trying to read a simple value from the webservice, however in my app i cannot extract the data values from the webservice xml file.

The data returned from the webservice looks like this:

enter image description here

In my app the data looks like this:

enter image description here

I only need to read out the value not the whole xml file. Any ideas how i can do this?

here is my code:

    MainActivity.java
package com.hit.resttest;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.os.AsyncTask;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener 
{

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) 
    {
     Button b = (Button)findViewById(R.id.my_button);
     b.setClickable(false);
     new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> 
    {
     protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException 
     {
      InputStream in = entity.getContent();
      StringBuffer out = new StringBuffer();
      int n = 1;
      while (n>0) {
       byte[] b = new byte[4096];
       n =  in.read(beer);
       if (n>0) out.append(new String(b, 0, n));
     }
     return out.toString();
    }

     @Override
     protected String doInBackground(Void... params) 
     {
      HttpClient httpClient = new DefaultHttpClient();
      HttpContext localContext = new BasicHttpContext();
      HttpGet httpGet = new HttpGet("http://192.168.1.118:8080/WebService/random");
      String text = null;
      try 
      {
       HttpResponse response = httpClient.execute(httpGet, localContext);
       HttpEntity entity = response.getEntity();
       text = getASCIIContentFromEntity(entity);
      } 

      catch (Exception e) 
      {
       return e.getLocalizedMessage();
      }
      return text;
     }

     protected void onPostExecute(String results) 
     {
      if (results!=null) 
      {
       EditText et = (EditText)findViewById(R.id.my_edit);
       et.setText(results);
      }

       Button b = (Button)findViewById(R.id.my_button);
       b.setClickable(true);
      }
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }    
}

Solution

  • You have to parse the xml and set the values to the view. Here is the example.

    //import 
    import java.io.StringReader;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.InputSource;
    
    String test = "<Response><Terminal><Name>name</Name><Value>1</Value></Terminal></Response>";
        // parse
        try {
    
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            InputSource s = new InputSource(new StringReader(test)); 
            Document doc = dBuilder.parse(s);
    
            doc.getDocumentElement().normalize();
    
            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    
            NodeList nList = doc.getElementsByTagName("Terminal");
    
            System.out.println("----------------------------");
    
            for (int temp = 0; temp < nList.getLength(); temp++) {
    
                Node nNode = nList.item(temp);
    
                System.out.println("\nCurrent Element :" + nNode.getNodeName());
    
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
    
                    Element eElement = (Element) nNode;
    
                    System.out.println("value : " + eElement.getElementsByTagName("Value").item(0).getTextContent());
    
                }
            }
            } catch (Exception e) {
            e.printStackTrace();
            }