Question

  • I'm implementing a yahoo integration in android for retrieving all contacts in my application.I got xml format url but i dont know how to do xml parsing in Android . Please can anyone help me.Here is my code .

    private static final String KEY_CONTACT="contacts";
    private static final String KEY_FIELDS="fields";
    private static final String KEY_TYPE="type";
    private static final String KEY_VALUE="value";

 private void getAllContacts() {

            String host_url = "http://social.yahooapis.com/v1/user/" + mUSER_GUID+ "/contacts";
            String nonce = ""+System.currentTimeMillis();
            String timeStamp = ""+(System.currentTimeMillis()/1000L);

            try{
                String params = 
                        ""+encode("oauth_consumer_key")+"=" + encode(CONSUMER_KEY)
                        + "&"+encode("oauth_nonce")+"="+encode(nonce)
                        + "&"+encode("oauth_signature_method")+"="+encode("HMAC-SHA1")
                        + "&"+encode("oauth_timestamp")+"="+encode(timeStamp)
                        + "&"+encode("oauth_token")+"="+ACCESS_TOKEN
                        + "&"+encode("oauth_version")+"="+encode("1.0");


                String baseString = encode("GET")+"&"+encode(host_url)+"&"+encode(params);
               String signingKey = encode(CONSUMER_SECRET)+"&"+encode(ACCESS_TOKEN_SECRET);
              String lSignature = computeHmac(baseString, signingKey);
             lSignature = encode(lSignature);

                      String lRequestUrl = host_url
                                    + "?oauth_consumer_key="+CONSUMER_KEY
                                    + "&oauth_nonce="+nonce
                                    + "&oauth_signature_method=HMAC-SHA1"
                                    + "&oauth_timestamp="+timeStamp
                                    + "&oauth_token="+ACCESS_TOKEN
                                    + "&oauth_version=1.0"
                                    + "&oauth_signature="+lSignature
                                    ;

                HttpGet httpget = new HttpGet(lRequestUrl);
                HttpClient httpclient = new DefaultHttpClient();

               ResponseHandler<String> responseHandler = new BasicResponseHandler();


                String responseBody = httpclient.execute(httpget, responseHandler);

                SAXParserFactory spf=SAXParserFactory.newInstance();
                SAXParser sp=spf.newSAXParser();

                // Get the XMLReader of the SAXParser we created. 
                XMLReader xr=sp.getXMLReader();

                InputSource inputSource = new InputSource();
                inputSource.setEncoding("UTF-8");
                inputSource.setCharacterStream(new StringReader(responseBody));

                 xr.parse(inputSource);
                   Log.e("XML","READER");
     catch(Exception e)
            {
                e.printStackTrace();
                Log.e(TAG, "error while fetching user contacts");
            }

        }


 Thanks to you .               
Was it helpful?

Solution

If you use SAX, you have to create a DefaultHandler and override its methods for parsing your XML file.

Here an example http://www.mkyong.com/java/how-to-read-xml-file-in-java-sax-parser/

OTHER TIPS

Android itself recommending XmlPullParser, which is an efficient and maintainable way to parse XML on Android.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top