国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實現(xiàn)授權訪問網(wǎng)頁的方法

2020-04-11 11:47:48
字體:
供稿:網(wǎng)友

本文實例講述了Android授權訪問網(wǎng)頁的實現(xiàn)方法,即使用Webview顯示OAuth Version 2.a ImplicitGrant方式授權的頁,但是對于移動終端不建議使用Authorize code grant方式授權

具體功能代碼如下所示:

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方式授權的頁 * (移動終端不建議使用Authorize code grant方式授權 * 本類使用方法 * 調(diào)用本類的地方請?zhí)砑尤缦麓a * //請將OAuthV2Activity改為類的類名 * Intent intent = new Intent(OAuthV2Activity.this, OAuthV2AuthorizeWebView.class); * intent.putExtra("oauth", oAuth); //oAuth為OAuthV2類的實例,存放授權相關信?? * startActivityForResult(intent, myRrequestCode); //請設置合適的requsetCode * 重寫接收回調(diào)信息的方 * if (requestCode==myRrequestCode) { //對應之前設置的的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);  }}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 应用必备| 城步| 赤峰市| 延庆县| 漳平市| 南安市| 股票| 兴仁县| 星座| 南投县| 岱山县| 台山市| 旺苍县| 冕宁县| 武宣县| 湖南省| 二连浩特市| 嵩明县| 临武县| 平度市| 长兴县| 泽州县| 措勤县| 宝山区| 浪卡子县| 三门县| 肥东县| 宝清县| 新邵县| 贵定县| 凤山县| 精河县| 平乡县| 集贤县| 芜湖市| 平昌县| 左权县| 大安市| 南澳县| 安阳市| 得荣县|