Android實(shí)現(xiàn)授權(quán)訪問網(wǎng)頁的方法
本文實(shí)例講述了Android授權(quán)訪問網(wǎng)頁的實(shí)現(xiàn)方法,即使用Webview顯示OAuth Version 2.a ImplicitGrant方式授權(quán)的頁,但是對(duì)于移動(dòng)終端不建議使用Authorize code grant方式授權(quán)。
具體功能代碼如下所示:
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.oauthv2.OAuthV2Client;
/**
* 使用Webview顯示OAuth Version 2.a ImplicitGrant方式授權(quán)的頁
* (移動(dòng)終端不建議使用Authorize code grant方式授權(quán)
* 本類使用方法
* 調(diào)用本類的地方請(qǐng)?zhí)砑尤缦麓a
* //請(qǐng)將OAuthV2Activity改為類的類名
* Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class);
* intent.putExtra("oauth", oAuth); //oAuth為OAuthV2類的實(shí)例,存放授權(quán)相關(guān)信??
* startActivityForResult(intent, myRrequestCode); //請(qǐng)?jiān)O(shè)置合適的requsetCode
* 重寫接收回調(diào)信息的方
* if (requestCode==myRrequestCode) { //對(duì)應(yīng)之前設(shè)置的的myRequsetCode
* if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE) {
* //取得返回的OAuthV2類實(shí)例oAuth
* oAuth=(OAuthV2) data.getExtras().getSerializable("oauth");
* }
* }
* @see android.app.Activity#onActivityResult(int requestCode, int resultCode, Intent data)
*/
public class MyWebView extends Activity {
public final static int RESULT_CODE = 2;
private OAuthV2 oAuth;
private final String TAG = "MyWebView";
private WebView mWebView;
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview_qq);
mWebView = (WebView) findViewById(R.id.qq_mywebview);;
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
Intent intent = this.getIntent();
oAuth = (OAuthV2) intent.getExtras().getSerializable("oauth");
String urlStr = OAuthV2Client.generateImplicitGrantUrl(oAuth);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
mWebView.requestFocus();
mWebView.loadUrl(urlStr);
System.out.println(urlStr.toString());
Log.i(TAG, "WebView Starting....");
WebViewClient client = new WebViewClient() {
/* 回調(diào)方法,當(dāng)頁面加載時(shí)執(zhí)行*/
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i(TAG, "WebView onPageStarted...");
Log.i(TAG, "URL = " + url);
if (url.indexOf("access_token=") != -1) {
int start=url.indexOf("access_token=");
String responseData=url.substring(start);
OAuthV2Client.parseAccessTokenAndOpenId(responseData, oAuth);
Intent intent = new Intent();
intent.putExtra("oauth", oAuth);
setResult(RESULT_CODE, intent);
finish();
}
super.onPageStarted(view, url, favicon);
Log.i(TAG, "999999999");
}
/* TODO Android2.2及以上版本才能使用該方法,目前https://open.t.qq.com中存在http資源會(huì)引起sslerror,待網(wǎng)站修正后可去掉該方*/
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
if ((null != view.getUrl()) && (view.getUrl().startsWith("https://open.t.qq.com"))) {
handler.proceed();// 接受證書
} else {
handler.cancel(); // 默認(rèn)的處理方式,WebView變成空白
}
// handleMessage(Message msg); 其他處理
}
};
mWebView.setWebViewClient(client);
}
}
- Android中Webview打開網(wǎng)頁的同時(shí)發(fā)送HTTP頭信息方法
- Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
- Android編程實(shí)現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器實(shí)例
- Android中訪問證書有問題的SSL網(wǎng)頁的方法
- Android下保存簡單網(wǎng)頁到本地(包括簡單圖片鏈接轉(zhuǎn)換)實(shí)現(xiàn)代碼
- 通過Html網(wǎng)頁調(diào)用本地安卓(android)app程序代碼
- Android中獲取網(wǎng)頁表單中的數(shù)據(jù)實(shí)現(xiàn)思路及代碼
- Android中實(shí)現(xiàn)地址欄輸入網(wǎng)址能瀏覽該地址網(wǎng)頁源碼并操作訪問網(wǎng)絡(luò)
- android 封裝抓取網(wǎng)頁信息的實(shí)例代碼
- Android 自定義標(biāo)題欄 顯示網(wǎng)頁加載進(jìn)度的方法實(shí)例
- Android使用WebView.loadUri()打開網(wǎng)頁的方法
相關(guān)文章
Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)代碼
本篇文章對(duì)Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實(shí)現(xiàn)用實(shí)例進(jìn)行了介紹。需要的朋友參考下2013-05-05
androidstudio3.0使用butterknife報(bào)錯(cuò)解決的解決方法
這篇文章主要介紹了androidstudio3.0使用butterknife報(bào)錯(cuò)解決的解決方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-01-01
Android 自定義圓形帶刻度漸變色的進(jìn)度條樣式實(shí)例代碼
這篇文章主要介紹了Android 自定義圓形帶刻度漸變色的進(jìn)度條的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android下2d物理引擎Box2d用法簡單實(shí)例
這篇文章主要介紹了Android下2d物理引擎Box2d用法,實(shí)例分析了在Android平臺(tái)上使用Box2d的基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07

