Ss

if (session, equa Is <'"') ) return ""; else return session; I

K^Ji^fi* Shared Preferences 'I'ilrSE&ftMV T W/^SttW session key, to IRS ft W\ik m*: burnt-fa, W'JiSNiiCt key ffiffiU

mifcfixmmiMm rfatmiim*, «iri#«£»wj showAieitDiaiogo

private void showAlertDialog() {

final AlertDialoq a]ert = new AlertDia log.Builder(this).create(); alert. setTit le ("¡5CS") ;

alert. setMessage (getString (R. string . app start.) ) ; alert. set But tori ( , new Diaioglnterface.Or.ClickListener () {

public void ohClick(Dialoglnterface dialog, int whichButton) ( alert,.dismiss(); try i cpenWebBrowser(Tools.get AuthorizeUrl(getToken() )); } catch (NoSuchAigorithmExcept.ion e) (

e.printStackTrace(); }

alert.setButton2 (">ifl0", new Dialoglnterface.OnCl¿CkListener() { public void onClick (Diaioglnterface dialog, .int whichButton) I alert.dismiss(); try |

openWebBrowser (Tools . getRegUrl (getToker. () ) ) ; I catch (NoSucnAlgorithmException e) { e.printStackTrace(); ) )

^.lift'^tt«!»«««-^ openWebBrowser()7jviJTJf »ItfifeRffiiltfr&msSLSU, ]«fc*fciw 15-6 rr^o

WMfcHJ i&^y.ra»fn]KKr,W«PBBPnf5 4n*fflpiE »Wyobo«!**;}. UliJiiJWiiil]

tst*«fliwiesi.

11i mtiltn, Wikif openWebBrowser(Tools.getAuthorize Url(get

Token()))&&fW. 5t# openWebBrowserOftj&fJt, ftWJn K0T75:

// njtm&m, protected void openWebBrowser(String url) ( Log. i (TAG, "about to launch browser, url: " + url); a 89 16-6

Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url));

startActivity(i); }

l.mm^m^A tUrUM:, ) t Activity. ScA^JfthkW^ft, «^iH*. ftff] 4--gfi-Tools.getAuthorizeUrl(getToken())i5yhff<J'i;fI. . JfcflgctAuthorizeUrl()

// ttifefflP&tXURL

public static String getAuthorizeUrI(String token) ( String url = "";

url - PreferencesUti 1 .baseUrl + PreferencesUti1.authorizeUrl + w?token»" + tokens-return url; I

S&fftttftMltli:. K-'l' PreferencesUtil f YOBO & I 1'I^J (in API Key ^o

'Jg^tfgetTokenO^», YOBO API M«MS, «JR-^iSl»J token, token ¿ettlSifli.^-^iftiJStijF. /MifrW token,«Mffiiffla^ttS&uEWiirH token &JR t access token, KiffJaccess token ifrfojrtj/"1 WftWftlK. ¡Hi*getToken()

HffiSMttrr. «»411 FW/K: // SRttfflft private String getTokenO throws NoSuchAlgorithmException { String token = try {

// l.tt&ffiSftfMS

long timestamp = System.currentTimeMi11 is();

String nonce = Tools.mdb(String.valueOf(System.currentTimeMi1lis()) + PreferencesUtil.api_secret);

Hashtable<String, Strinq> params new Hashtable<String, String>(); params.put("api key", Preference^Jtil.^pi key); params.put("nonce", nonce);

params.put("timestamp", String.valueOf(timestamp)); //

String api sig Tools . indb (Tools . sig params (pa rams) + PreferencesUtil.api_secret); // 3.^Dapi_sig

String params_str = Tools.paramsToString(params) + "api sig-" + api_sig;

// RequestTokenJSf*

String url get = PrefereneesUti1.baseUrl + PreferencesUtil.tokenUrl; Log.i("url get", url get);

String result = Tools.sendGetRequest(url get, params str); // 6.*t&tjJi(ftXML&iT$#Tt ftl'j requestToken §*)it token - Tools.paxToken(result); // 7 .fci&'Mifcte*

SharedPreferences settings = getSharedPreferences( PreferencesUti1.preferencesSetting, 0);

settings.edit().putString(PreferencesDtil.token, token).commit(); // // URL

// url = PreferencesUtil.baseUrl + PreferencesUt11.authorizeUrl // ♦ "?token=" + token;

) catch (NoSuchAlgorithmException e) ( // TODO Auto-generated catch block e.printStackTrace ();

return token; )

SIMWiELt«»!«. tfftJftftiftifiBWIMft. iiftaBS—ttimcstamp. nonce WA api_kcy. VOBOAPI W^imi^JU Tools.sig_params (params) ttWfcl public static String sig_params(Hashtabie<String, ?> params) { Vector<String> v nev; Vector<String> (params. keySet ()) ; Collections.sort (v); // Display (sorted) hashtable. String result = "";

lor (Enumeration<String> e = v.elements(); e.hasMoreElements();) ( String key (String) e.nextElement(); String va1 = (String) params.get(key); result += key; resuiL i val;

Log.i("sig_params", "Key: " + key f ",Val: " + val);

Log . i ( "siu params" , "result:" + resuit.) ;

return result; }

key W<keyl><valuel><key2><value2> i&ffflfctrjS. #iSLM

tit-f'T'lSifj'Pffi'^ii^i^ffii Tools.md5 (Tools.sig params (params) + PreferencesUtil.api secret)

public static String md5(String s) throws NoSuchAlgorithmException ( System.out.println(System.currentTimeMillis()); MessageDigest m = Message Digest. get Instance ( "MD5" ) ; m.update(s.getBytes(), 0, s.length());

String tndb str = new Bi glnteger (1, m.digest()).toString(16) ; Log.i("MD5", md5_st r); ii (md5 str.length() <32) ( md5 str = "0" . coricat (md!> st r) ; )

return md5_str; )

JM. ^ff^ifio S^'iHffl Tools.paramsToString()4:.^iffi1cfliJ#li» (WiUI KWdC

public static String paramsToString(Hashtable<String, ?> params) I Vector <St.r: ng> v = new Vector<String> (params . keySet ()) ; Col 1ections.sort(v);

// Display (sorted) hashtable. String result - "";

for (Enumeration<String • e v.elements(); e.hasMoreElements();) I

Stri r.q val (String) params.get(key);

result val;

Log.i("sig params", "Key: " + key + ",Val: " + val); I

Log.iC'sig params", "result:" + result); return result; )

J^l&^MPm.MJnim Tools.sendGetRequestCurl gel params str) tffj» V.mi'Rmn url get JifeJlk.ii» YOBOAPI mmmtViH token fftjfciil:, HTTP

wmmiiitMr. ^mm^mmfaresult«

Iflil'^KiyrriS^iBM»*» WiiMJffififfiUiM token BPnf, K^rr^ii^fi'linTrr^:

I// RequestToken i&EiKj XML, i^f'J Token toil public static String paxToken(String xml) | String regEx = "<token> (.♦-?) </token>" ; Pattern p = Pattern.compile (regEx.) ; Matcher m p.matcher(xml); boolean rs = m.findO; if (rs) {

System.out. print In ("^¿SivMM: " 4 m.startO + "t&^t&B*::" + m.end() » + m.group () );

return m.group(1); 1

ijeiaw XML («AfMffl. mmmiToken iHjffi, *t fKl XML, mm'UmmH token jsmmwmmr SharedPreferences 'I'. iK

SharedPreferences settings - getSharedPreferences( PreferencesUtil.preferencesSetting, 0);

seLtings.edit().putString(PreferencesUtil.token, token).commit();

iA'll ri'yt^iR SharedPreferences JftiffittftK¥i]A<j token tf-figfc*,, y. I-.JJIM'M^IT WN token. token »##11] YOBO API H^Mtt tX'/llftl I ¡I'i^llir'«^. HiM£ii'tf[ill'#JljWIS openWebBrowser()^?i, ft»ifiM 15-7

km 15-7 l.v.m it & j rj > ■ ■ $ &mti®, ^jh^tm? "few mun. um&mmvjntt

YOBO IW-J, wijitYi " A^^irjfti^iiufii" xsemn

■tm. H&jiifs&trjf&mmi. a^ftfij it-ftwmwj

Was this article helpful?

0 0

Post a comment