Android enables access to Web pages _android

Source: Internet
Author: User
Tags oauth

This article describes how Android grants access to Web pages by using WebView to display OAuth Version 2.a implicitgrant-licensed pages , but not recommended for mobile terminals authorize Code grant mode authorization .

The specific functional code looks like this:

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;
 /** * Use WebView to display OAuth Version 2.a implicitgrant Licensing page * (Mobile terminals do not recommend the use of authorize code grant authorization * This class uses the method * Call this class place please add the following code
 *//Please change the oauthv2activity to class name * Intent Intent = new Intent (oauthv2activity.this, Oauthv2authorizewebview.class); * Intent.putextra ("OAuth", OAuth);
 OAuth is an example of a OAuthV2 class that stores authorization related letters?? * Startactivityforresult (Intent, Myrrequestcode); Please set the appropriate Requsetcode * to override the Myrequsetcode * if (requestcode==myrrequestcode) {///* if (Resultcode==oauth) that received the callback information. V2authorizewebview.result_code) {*//Get returned OAUTHV2 class instance OAuth * OAuth= (OAuthV2) Data.getextras (). Getserializable ("OAuth"); *} * @see android.app.activity#onactivityresult (int requestcode, int resultcode, Intent data)/public Class I
  WebView 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 (Savedinstancesta
    TE);
    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 () {/* callback method executing/@Override public void onpagestarted When the page is loaded (Webvie
        W 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 and the above version to use this method, the current https://open.t.qq.com in the presence of HTTP resources will cause sslerror, the site can be modified to remove the party * * public void ONRECEIVEDSS Lerror (WebView view, Sslerrorhandler handler, SSLError error) {if (null!= view.geturl ()) && (View.geturl (). StartsWith ("https://open.t.qq.com")) { Handler.proceed ();//Accept Certificate} else {handler.cancel ();//default processing, WebView to blank}//ha Ndlemessage (msg);
    Other processing}};
  Mwebview.setwebviewclient (client); }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.