demo工程的清单文件及activity中api代码简单示例

第一步注册一个账户,并创建一个应用。获取app ID与 app Key。

第二步下载sdk

第三步新建工程,修改清单文件,导入相关的sdk文件及调用相应的api搞定。

3.1 修改清单文件,主要是加入一个webview的activity

[html]
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
3.2 将Android_SDK_v1.2.jar与httpmime-4.1.3.jar导入libs中就好。

3.3 在需要三方登录的地方,调用相应的api即可。

下面是小demo工程的清单文件及activity中api代码简单示例。
[html]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.chesterweibodemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<!-- 允许网络访问 -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- demo activity,调用api -->
<activity
android:name=".ChesterWeiboDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- OAuth Version 2. 使用 WebView 辅助进行ImplicitGrant方式授权必须 -->
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.chesterweibodemo"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<!-- 允许网络访问 -->
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<!-- demo activity,调用api -->
<activity
android:name=".ChesterWeiboDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- OAuth Version 2. 使用 WebView 辅助进行ImplicitGrant方式授权必须 -->
<activity
android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
ChesterWeiboDemoActivity 代码如下:
[java]
package com.test.chesterweibodemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;
import com.tencent.weibo.api.UserAPI;
import com.tencent.weibo.constants.OAuthConstants;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;
/**
* @author chensf5 2013-1-22
*/
public class ChesterWeiboDemoActivity extends Activity {
private static final String TAG = "ChesterWeiboDemoActivity";
private OAuthV2 oAuth;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//验证回调url地址,随便填写个 http://www.tencent.com/zh-cn/index.shtml
oAuth = new OAuthV2("http://apk.91.com/Soft/Android/com.palmit.player-1-1.0.html");
oAuth.setClientId("801297210"); // 2881064151
oAuth.setClientSecret("d163aeecdc7a9e5a601b03d66d4265be"); // be1dd1410434a9f7d5a2586bab7a6829
Intent intent = new Intent(ChesterWeiboDemoActivity.this,
OAuthV2AuthorizeWebView.class);
intent.putExtra("oauth", oAuth);
startActivityForResult(intent, 1); //开启webview加载个html页面
}
private Handler handler = new Handler() {
public void handleMessage(Message msg) {
if (null != msg.obj && msg.obj instanceof String) {
String response = (String) msg.obj;
((TextView) findViewById(R.id.tv_content)).setText(response
+ "\n");
Log.i(TAG, response);
Log.i(TAG + "----------",
"redirectUri:" + oAuth.getRedirectUri() + ",clientId:"
+ oAuth.getClientId() + ",clientSecret:"
+ oAuth.getClientSecret() + ",responseType:"
+ oAuth.getResponeType() + ",type:"
+ oAuth.getType() + ",authorizeCode:"
+ oAuth.getAuthorizeCode() + ",accessToken:"
+ oAuth.getAccessToken() + ",expiresIn:"
+ oAuth.getExpiresIn() + ",grantType:"
+ oAuth.getGrantType() + ",refreshToken:"
+ oAuth.getRefreshToken() + ",openid:"
+ oAuth.getOpenid() + "," + oAuth.getOpenkey());
}
};
};
protected void onActivityResult(int requestCode, int resultCode,
final Intent data) {
if (requestCode == 1) {
if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
new Thread() {
public void run() {
oAuth = (OAuthV2) data.getExtras().getSerializable("oauth");
// 调用API获取用户信息
UserAPI userAPI = new UserAPI(
OAuthConstants.OAUTH_VERSION_2_A);
try {
String response = userAPI.info(oAuth, "json");// 获取用户信息
Message msg = handler.obtainMessage();
msg.obj = response;
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
userAPI.shutdownConnection();
};
}.start();
} else {
Log.i(TAG, "返回过来的不对");
}
} else {
Log.i(TAG, "没有授权可拿");
}
}
}

上一篇:Java 多线程基础(三) start() 和 run()


下一篇:REST认识