Пример #1
0
 public void onClick(View v) {
   Intent intent;
   switch (v.getId()) {
     case R.id.btnRequest:
       Log.i(TAG, "---------Step1: Get requestToken--------");
       try {
         // 向腾讯微博开放平台请求获得未授权的Request_Token
         oAuth = OAuthV1Client.requestToken(oAuth);
       } catch (Exception e) {
         e.printStackTrace();
       }
       textRequest.append(
           "\nrequest_token:\n"
               + oAuth.getOauthToken()
               + "\nrequest_token_secret:\n"
               + oAuth.getOauthTokenSecret());
       break;
     case R.id.btnAuthorize:
       Log.i(TAG, "---------Step2: authorization--------");
       Log.i(TAG, "start WebView intent");
       intent =
           new Intent(
               OAuthV1Activity.this,
               OAuthV1AuthorizeWebView.class); // 创建Intent,使用WebView让用户授权
       intent.putExtra("oauth", oAuth);
       startActivityForResult(intent, 1);
       break;
     case R.id.btnAccess:
       Log.i(TAG, "---------Step3: getAccessToken--------");
       try {
         oAuth = OAuthV1Client.accessToken(oAuth);
         /*
          * 注意:此时oauth中的Oauth_token和Oauth_token_secret将发生变化,用新获取到的
          * 已授权的access_token和access_token_secret替换之前存储的未授权的request_token
          * 和request_token_secret.
          */
       } catch (Exception e) {
         e.printStackTrace();
       }
       textAccess.append(
           "\naccess_token:\n"
               + oAuth.getOauthToken()
               + "\naccess_token_secret:\n"
               + oAuth.getOauthTokenSecret());
       break;
     case R.id.btnAPItest:
       Log.i(TAG, "---------Step4: Test API V1--------");
       intent =
           new Intent(
               OAuthV1Activity.this,
               WeiBoAPIV1Activity.class); // 创建Intent,转到调用Qweibo API的Activity
       intent.putExtra("oauth", oAuth);
       startActivity(intent);
       break;
   }
 }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_PROGRESS);
    super.onCreate(savedInstanceState);
    WebSettings settings = webview.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setSupportZoom(true);
    webview.requestFocus();
    String authornizeUrl =
        OAuthConstants.OAUTH_V1_AUTHORIZE_URL + "?oauth_token=" + oauth.getOauthToken();
    webview.loadUrl(authornizeUrl);
    webview.setWebChromeClient(
        new WebChromeClient() {

          @Override
          public void onProgressChanged(WebView view, int newProgress) {
            pb.setProgress(newProgress);
            if (newProgress == 100) {
              pb.setVisibility(View.GONE);
            }
          }
        });
    client =
        new WebViewClient() {

          @Override
          public void onPageStarted(WebView view, String url, Bitmap favicon) {
            if (url.indexOf("checkType=verifycode") != -1) {
              int start = url.indexOf("checkType=verifycode&v") + 23;
              int end = start + 6;
              String verifyCode = url.substring(start, end);
              oauth.setOauthVerifier(verifyCode);
              Intent intent = new Intent();
              intent.putExtra("oauth", oauth);
              setResult(RESULT_CODE, intent);
              view.destroyDrawingCache();
              finish();
            }
          }
        };
    webview.setWebViewClient(client);
  }