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等。
*****************************失恋分割线*************************
没能好好珍惜你是我对我们之间最大的遗憾
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值