Android實現(xiàn)授權(quán)訪問網(wǎng)頁的方法
本文實例講述了Android授權(quán)訪問網(wǎng)頁的實現(xiàn)方法,即使用Webview顯示OAuth Version 2.a ImplicitGrant方式授權(quán)的頁,但是對于移動終端不建議使用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)的頁
* (移動終端不建議使用Authorize code grant方式授權(quán)
* 本類使用方法
* 調(diào)用本類的地方請?zhí)砑尤缦麓a
* //請將OAuthV2Activity改為類的類名
* Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class);
* intent.putExtra("oauth", oAuth); //oAuth為OAuthV2類的實例,存放授權(quán)相關(guān)信??
* startActivityForResult(intent, myRrequestCode); //請設(shè)置合適的requsetCode
* 重寫接收回調(diào)信息的方
* if (requestCode==myRrequestCode) { //對應(yīng)之前設(shè)置的的myRequsetCode
* if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE) {
* //取得返回的OAuthV2類實例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)方法,當頁面加載時執(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資源會引起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(); // 默認的處理方式,WebView變成空白
}
// handleMessage(Message msg); 其他處理
}
};
mWebView.setWebViewClient(client);
}
}
- Android中Webview打開網(wǎng)頁的同時發(fā)送HTTP頭信息方法
- Android Webview添加網(wǎng)頁加載進度條實例詳解
- Android編程實現(xiàn)網(wǎng)絡(luò)圖片查看器和網(wǎng)頁源碼查看器實例
- Android中訪問證書有問題的SSL網(wǎng)頁的方法
- Android下保存簡單網(wǎng)頁到本地(包括簡單圖片鏈接轉(zhuǎn)換)實現(xiàn)代碼
- 通過Html網(wǎng)頁調(diào)用本地安卓(android)app程序代碼
- Android中獲取網(wǎng)頁表單中的數(shù)據(jù)實現(xiàn)思路及代碼
- Android中實現(xiàn)地址欄輸入網(wǎng)址能瀏覽該地址網(wǎng)頁源碼并操作訪問網(wǎng)絡(luò)
- android 封裝抓取網(wǎng)頁信息的實例代碼
- Android 自定義標題欄 顯示網(wǎng)頁加載進度的方法實例
- Android使用WebView.loadUri()打開網(wǎng)頁的方法
相關(guān)文章
Android 實現(xiàn)永久性開啟adb 的root權(quán)限
這篇文章主要介紹了Android 實現(xiàn)永久性開啟adb 的root權(quán)限,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Android中Toolbar隨著ScrollView滑動透明度漸變效果實現(xiàn)
這篇文章主要介紹了Android中Toolbar隨著ScrollView滑動透明度漸變效果實現(xiàn),非常不錯,具有參考借鑒價值,需要的的朋友參考下2017-01-01
使用RecyclerView實現(xiàn)瀑布流高度自適應(yīng)
這篇文章主要為大家詳細介紹了使用RecyclerView實現(xiàn)瀑布流高度自適應(yīng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-09-09
Android開發(fā)必備知識 為什么說Kotlin值得一試
為什么說值得一試,這篇文章主要為大家詳細介紹了Android開發(fā)必備知識,Kotlin的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05
Android開發(fā)中Looper.prepare()和Looper.loop()
Looper用于封裝了android線程中的消息循環(huán),默認情況下一個線程是不存在消息循環(huán)(message loop)的,具體調(diào)用方法大家可以通過本文學習2016-11-11

