2013-05-06 39 views
2
  • 我執行在android系統雅虎整合檢索我application.I所有 接觸了XML格式的url,但我不知道如何 做XML解析Android中。請任何人都可以幫助我。這裏是我的 的代碼。

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 .    
+0

你應該粘貼你得到你的XML響應。 – 2013-05-06 07:54:35

回答

0

的Android本身推薦XmlPullParser,其是解析在Android XML有效和維護的方法。