0

I am new to android programming.My requirement is to invoke the web services.I successfully got the response from web services.how to parse the response in android.Give me solution.

This is the code for getting response:

 HttpResponse response = httpclient.execute(httppost);
           String str=response.getStatusLine().toString();

           System.out.println("========URL STATUS========"+str);

           HttpEntity r_entity = response.getEntity();

           if( r_entity != null ) {
               result = new byte[(int) r_entity.getContentLength()];
               if(r_entity.isStreaming()) {
                    is = new DataInputStream(r_entity.getContent());
                   is.readFully(result);

               }
           }

           httpclient.getConnectionManager().shutdown();
           String responsedata= (new String(result).toString());
2
  • what type format you get response ? Commented Mar 21, 2013 at 12:38
  • First of all take a look at EntityUtils.toString(), and secondly: use same xml parser like SimpleXML or something else Commented Mar 21, 2013 at 12:39

3 Answers 3

3

The below sample is for dom parser.

 DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
 DocumentBuilder db = dbf.newDocumentBuilder();
 InputSource is = new InputSource();
 StringReader sr=new StringReader(result.toString());
 is.setCharacterStream(sr);
 Document doc = db.parse(is);
 NodeList nodes = doc.getElementsByTagName("your root tag");

 //get your other tag elements.

http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/. Example of dom parser.

http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/. Example of sax parser.

Dom is w3c based parser. Dom is slower than sax cause it uses tree node and has to be in mmeory. So parsing large data using DOM parser is not recommended.

SAX on the other hand is faster than dom. Recommended for large xml data.

The above links gives you examples of both. Use any of the above parser to parse and get values from the xml tags.

Sign up to request clarification or add additional context in comments.

2 Comments

I parse the result but it is giving entire result as one string.how to split by xml tag wise?
u have to parse the xml using sax or dom parser or simple xml parser
0

You can try to use SimpleXmlParser. It's a native android class. It's simpler than DOM xml parser.

Comments

0

what you need is simply a android xml parse library. There are plenty of xml parse for android.

official tutorial

there is also a article "comparing methods for xml parsing in android"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.