博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
webview使用
阅读量:6691 次
发布时间:2019-06-25

本文共 4031 字,大约阅读时间需要 13 分钟。

hot3.png

package tree.love.weiboFinishing.test;  
  
import java.util.List;  
  
import tree.love.weiboFinishing.OAuthConstant;  
import tree.love.weiboFinishing.R;  
import weibo4Android.Paging;  
import weibo4android.Status;  
import weibo4android.Weibo;  
import weibo4android.WeiboException;  
import weibo4android.http.AccessToken;  
import weibo4android.http.RequestToken;  
import android.app.Activity;  
import android.graphics.Bitmap;  
import android.net.Uri;  
import android.os.Bundle;  
import android.util.Log;  
import android.webkit.WebView;  
import android.webkit.WebViewClient;  
  
/** 
 * 用户授权页面 
 * 1.初始化OAuth对象 
 * 2.获取用户授权页面并填充至webView 
 * 3.根据载入的url判断匹配规则的结果执行跳转 
 *  
 * bywyu 
 *  
 */  
public class AuthorizationAct extends Activity  
{  
  
    private final String LOGTAG = "AuthorizationAct";  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState)  
    {  
        // TODO Auto-generated method stub   
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.authorization_ui);  
  
        // init   
        String url = null;  
          
        url = getIntent().getExtras().getString("webViewUrl");  
        Log.d(LOGTAG, "onCreat() [Authoriz] url = " + url);  
  
        initWebView(url);  
    }  
  
    private void initWebView(String url)  
    {  
        WebView authorizationView = (WebView) findViewById(R.id.authorizationView);  
        authorizationView.clearCache(true);  
        authorizationView.getSettings().setJavaScriptEnabled(true);  
        authorizationView.getSettings().setSupportZoom(true);  
        authorizationView.getSettings().setBuiltInZoomControls(true);  
        authorizationView.setWebViewClient(new WebViewC());  
        authorizationView.loadUrl(url);  
    }  
  
    class WebViewC extends WebViewClient  
    {  
        private int index = 0;  
  
        @Override  
        public boolean shouldOverrideUrlLoading(WebView view, String url)  
        {  
  
            view.loadUrl(url);  
            return true;  
        }  
  
        @Override  
        public void onPageStarted(WebView view, String url, Bitmap favicon)  
        {  
            super.onPageStarted(view, url, favicon);  
            Log.d(LOGTAG, "onPageStarted url = " + url);  
  
            /** 
             * url.contains(ConfigUtil.callBackUrl) 
             * 如果授权成功url中包含之前设置的callbackurl 
             * 包含:授权成功 
             *  
             *index == 0 
             *由于该方法onPageStarted可能被多次调用造成重复跳转 
             * 则添加此标示 
             */  
  
            if (url.contains(OAuthConstant.CALLBACK_URL) && index == 0)  
            {  
                index++;  
                Uri uri = Uri.parse(url);  
                // 匹配验证码   
                String oauth_verifier = uri.getQueryParameter("oauth_verifier");  
  
                RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();  
                AccessToken accessToken = null;  
                try  
                {  
                    accessToken = requestToken.getAccessToken(oauth_verifier);  
                    OAuthConstant.getInstance().setAccessToken(accessToken);  
                    Log.d(LOGTAG,"得到AccessToken的key和Secret,可以使用这两个参数进行授权登录了.\n Access token:\n"  
                                    + accessToken.getToken()  
                                    + "\n Access token secret:\n"  
                                    + accessToken.getTokenSecret());  
                }  
                catch (WeiboException e)  
                {  
                    e.printStackTrace();  
                }  
  
                testWeiboData();  
            }  
        }  
  
        @Override  
        public void onPageFinished(WebView view, String url)  
        {  
            super.onPageFinished(view, url);  
        }  
    }  
      
    public void testWeiboData()  
    {  
        Weibo weibo = OAuthConstant.getInstance().getWeibo();  
        weibo.setToken(OAuthConstant.getInstance().getToken(),  
                OAuthConstant.getInstance().getTokenSecret());  
        List<Status> friendsTimeline;  
        try  
        {  
            friendsTimeline = weibo.getTrendStatus("seaeast",  
                    new Paging(1, 20));  
            StringBuilder stringBuilder = new StringBuilder("");  
            for (Status status : friendsTimeline)  
            {  
                stringBuilder.append(status.getUser().getScreenName()  
                        + "说:\n"  
                        + status.getText()  
                        + "\n--------------------------------------------------\n");  
            }  
            Log.v(LOGTAG,stringBuilder.toString());  
        }  
        catch (WeiboException e)  
        {  
            e.printStackTrace();  
        }  
    }  
}  
看log,

至于说获取到access token之后要做的事情,就由各位有创意的开发者来决定了,保存access token和Access token secret.跳转到主页,

requestToken = weibo.getOAuthRequestToken(OAuthConstant.CALLBACK_URL);  

                    OAuthConstant.getInstance().setRequestToken(requestToken);  
                    String authenticationURL = requestToken.getAuthenticationURL();  
                    String authorizationURL = requestToken.getAuthorizationURL();  
                    Log.v(TAG , "authenticationURL="+authenticationURL);  
                    Log.v(TAG, "authorizationURL="+authorizationURL);  
                      
                    Intent intent = new Intent(MainActivity.this,AuthorizationAct.class);  
                    intent.putExtra("webViewUrl", authenticationURL);  
                    startActivity(intent);  
0.2 用户登陆授权,获取access token,从WebView中截获验证码oauth_verifier

转载于:https://my.oschina.net/handsomeban/blog/175776

你可能感兴趣的文章
Linux 文件操作监控inotify功能及实现原理【转】
查看>>
linux arm的存储分布那些事之一
查看>>
Spring下redis的配置
查看>>
vs2010在进行数据架构比较时报'text lines should not be null'错误
查看>>
13th_july_scrapy-splash
查看>>
新浪招聘的图片滚动控制JS效果
查看>>
jeecg入门操作—表单界面
查看>>
如何折分字符串技巧讨论-总结
查看>>
第24课 经典问题解析二
查看>>
Material Design 组件之NavigationView
查看>>
【Android】3.13 路径规划功能
查看>>
工作中记录一 list转树
查看>>
spring踩坑
查看>>
Delphi按名字调用方法高级解决方案
查看>>
关于对象与类型
查看>>
转:程序员面试什么最重要
查看>>
团队项目(六)- 事后诸葛亮分析(江山代有才人秃)
查看>>
Linux基本命令(二)-----vim相关命令
查看>>
一些常用的正则表达式
查看>>
UI设计,使用感知分层技术
查看>>