第一步注册一个账户,并创建一个应用。获取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, "没有授权可拿");
}
}
}

demo工程的清单文件及activity中api代码简单示例的更多相关文章

  1. Android中ProgressDialog的简单示例

    网上一般对进度条的示例都是如何显示,没有在任务结束如何关闭的文章,参考其他文章经过试验之后把整套进度条显示的简单示例如下: 建立android工程等工作都略去,Google一下就可以了. 下面来介绍主 ...

  2. Windchill 配置LOG文件,使开发中的代码能显示打印的信息

    如开发代码的类HomeLogic.java, 包路径在pnt.report.home 需求:需监控此类的打印数据 方法:配置D:\ptc\Windchill_10.1\Windchill\codeba ...

  3. C#中汉字排序简单示例(拼音/笔划)

    可以按照区域语言修改排序规则. class Program { static void Main(string[] args) { string[] arr = { "趙(ZHAO)&quo ...

  4. 关于SQL Server中存储过程在C#中调用的简单示例

    目录 0. 简介 1. 语法细节 2. 示例1:模拟转账 3. 示例2:测试返回DataTable 4. 源代码下载 shanzm-2020年5月3日 23:23:44 0. 简介 [定义]:存储过程 ...

  5. JAVA中CountDownLatch的简单示例

    public static void main(String[] args) throws InterruptedException { CountDownLatch latch =new Count ...

  6. Android的学习之路(四)项目中清单文件的学习和android中经常使用的显示单位

    1.所谓的清单文件就是项目中的AndroidManifest.xml文件.这个文件但是有大用处的.比方:app的名字,图标.app支持的版本号app的包名等等.以下我就介绍下这个清单文件的各个參数的作 ...

  7. 01_创建一个新的activity&activity配置清单文件

    今天开始学四大组件.今天是学Activity,然后是广播接收者,然后是服务,然后是内容提供者.四大组件,咱们一天一个.Activity就是跟用户交互的界面,大部分的应用都不会只有这么一个界面.创建多个 ...

  8. Android清单文件详解(三)----应用程序的根节点<application>

    <application>节点是AndroidManifest.xml文件中必须持有的一个节点,它包含在<manifest>节点下.通过<application>节 ...

  9. Android清单文件具体解释(三)----应用程序的根节点&lt;application&gt;

    <application>节点是AndroidManifest.xml文件里必须持有的一个节点,它包括在<manifest>节点下.通过<application>节 ...

随机推荐

  1. cve-2015-1635漏洞分析

    上周(2015.4.15)爆出的cve-2015-1635漏洞,遂分析一番,留作记录.使用poc如下. wget 192.168.16.168/welcome.png --debug --header ...

  2. 读懂Android项目结构目录

    我们看到下图:当我们创建了第一Android项目的时候有没有被吓到.怎么这么多目录,好头晕啊!没事, 那我们今天就了解一下这些目录是做什么的: src: src 目录是放置我们所有 Java 代码的地 ...

  3. AngularJS 包含HTML文件

    类似于python tornado的include方法,同样是可以在一个html文件中加载另外一个html文件,这样可以不用重复的写一些几乎不改变的代码. 首先创建两个文件,然后代码如下: <! ...

  4. Linux(CentOS)中安装MongoDB

    1. 下载对应操作系统和机器的版本 网址: https://www.mongodb.com/download-center?jmp=docs 我的系统为CentOS-6.6 ,64位,所以选择红帽子版 ...

  5. MapReduce应用案例--简单的数据去重

    1. 设计思路 去重,重点就是无论某个数据在文件中出现多少次,最后只是输出一次就可以. 根据这一点,我们联想到在reduce阶段数据输入形式是 <key, value list>,只要是k ...

  6. Oracle 查询性能优化实践

      1.索引使用原则   不要对索引使用全模糊,但是 LIKE 'asdf%'是可以的,即不要Contains,可用StartWith 不要对索引进行函数,表达式操作,或者使用is null判断,否则 ...

  7. docker 1.0.0发布以及一个bug依赖apparmor_parser

    6月10号docker 1.0稳定版本发布,找了台ubuntu的机器,装了下 ubuntu version:12.04 docker version:1.0.0 装docker的步骤可以看官方文档:h ...

  8. BZOJ4118 : [Wf2015]Window Manager

    OPEN.CLOSE.RESIZE操作直接模拟即可. 对于MOVE,设$f_i$表示$i$号矩形的坐标,先无视边界通过DP求出每个矩形的坐标,再根据边界反向用第二次DP求出被移动矩形移动的真实距离,再 ...

  9. hdu1019 Least Common Multiple

    Problem Description The least common multiple (LCM) of a set of positive integers is the smallest po ...

  10. PHP面向对象学习六 多态

    OOP的模式并不仅仅是把很多函数和功能集合起来,目的而是使用类,继承,多态的方式描述我们生活中的一种情况.从而使得我们的代码更具有“物”的意义.帮助我们减少一些重复性的代码和条件语句的判断.   运算 ...