- 我在 android 中实现了一个 yahoo 集成,用于检索我的应用程序中的所有联系人。我得到了 xml 格式的 url,但我不知道如何在 Android 中进行 xml 解析。请任何人帮助我。这是我的代码。
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 .