android平臺下,淘寶客戶端開發之——登陸

1.囉嗦的理論寫在前面
在人們的印象中,各種社交網站的客戶端都把登陸作爲第一步,但對於淘寶客戶端,我們沒有必要這樣做。這是由於,好多懶人們懶得登陸或註冊,因此就不用你的客戶端了,直接影響將近三分之一的網站流量啊(這是看官方論壇中有人統計的)。因此我們在不得不登陸的時候再設置登陸這步,而不是放在客戶端的開始。
2.進行登陸的過程
淘寶的登陸爲了安全起見,規定以內嵌wap頁的形式網頁登陸,因此登陸這步我們就沒必要糾結於界面設計了,直接放一個WebView就OK了(url用Mars.jar包中的MtopLogin類的getLoginUrl()方法得到)。在頁面中實現登陸、授權後,MtopLogin類可以getTopSession(),這個參數在後續的得打用戶、進行買賣中將用到。
3.實現該過程的準備:
下載mars.alpha-v0.6([url]http://www.archermind.com/mtop[/url])
(1)新建android工程,在工程中導入mars.jar包(mars.alpha-v0.6\sdk\marr.jar)
(2)AndroidManifest.xml 中增加internet訪問權和手機硬件訪問權
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
(3)建res\layout\login.xml 將一個webview、一個按鈕、一個textview拖到一個LinearLayout中。
login.xml代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<WebView
android:id="@+id/wv1" android:layout_width="match_parent" android:layout_height="187dp">
</WebView>
<Button android:text="獲得用戶信息" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent"></TextView>
</LinearLayout>

(4)主要java代碼:
獲取web的url方法:

mWebView = (WebView) findViewById(R.id.webview);
// get WebSetting object,設置支持Javascript的參數
mWebView.getSettings().setJavaScriptEnabled(true);
// 頁面縮放設置
mWebView.getSettings().setBuiltInZoomControls(true);
// 使頁面獲得焦點
mWebView.requestFocus();

String mtopUrl = "http://api.m.taobao.com/rest/api2.do";
String v = "";
String appKey = "12311413";// 你的應用所申請的appkey和secret
String secret = "48d7e406fadaf1b1f7abb32a95a71c33";
ttid = "";
v = "androidClient";
mtopLogin = new MtopLogin(mtopUrl, v, imei, imsi, appKey, secret, ttid);
try {
try {
mWebView.loadUrl(mtopLogin.getLoginUrl());
} catch (ApiException e) {

e.printStackTrace();
}
} catch (JSONException e) {
mWebView.loadUrl("http://www.baidu.com");
}

/* response WebView event */
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});

button的onclick事件:

@Override
public void onClick(View v) {
try {
try {
mtopLogin.getUserSessionKey();
} catch (ApiException e) {
e.printStackTrace();
}
mtopLogin.getTopsession();
} catch (JSONException e1) {

e1.printStackTrace();
}

OpenServiceClient.init("http://gw.api.taobao.com/router/rest",
appKey, secret, getApplicationContext(), "", imei, imsi);//必須有這步才能調用UserDomainApi的方法

try {

User user = UserDomainApi.getUser("nick", "", mtopLogin.getTopsession());//通過session獲得user信息具體見doc中的文檔
text.setText(user.getNick() + user.getCreated());
} catch (ApiException e) {
text.setText("獲取失敗!");
}
}

(5)注意事項,獲取imei,imsi的參數在虛擬機與在真機中不同。
在真機測試中用:

TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei = telephonyManager.getDeviceId();
String imsi = telephonyManager.getSubscriberId();

來獲取。
虛擬機中這麼獲取會報錯的。
虛擬機中使用:

String imei = "89014103211118510720";//瞎編就行~~~~(>_<)~~~~
String imsi = "310260000000000";//瞎編就行O(∩_∩)O

(6)測試說明:
運行應用後,在webview中登陸並實現授權,然後點擊按鈕“獲取用戶信息”即可得到登陸的用戶名,證明登陸成功。
其實在執行 mtopLogin.getUserSessionKey();後已經登陸成功獲取到了其他操作所需的session,token等。
*****************************失戀分割線*************************
沒能好好珍惜你是我對我們之間最大的遺憾
發佈了20 篇原創文章 · 獲贊 2 · 訪問量 9023
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章