• android 新浪微博分享功能实现


    新浪微博分享功能  



    注册帐号后,创建应用后,会得到一个唯一的key和secret,如下图 







    我在工程中使用的是Oauth2.0隐式授权方式,所以需要在新浪微博开发者平台重定义地址,如下图 







    需要在工程中加入新浪微博demo中com.weibo.net里面的内容,如下图 







    需要修改Weibo.java里面的代码 

        private static String APP_KEY = ""; 

    private static String APP_SECRET = ""; 

    将里面的内容替换成你创建项目时候生成的key和secret 

    public staticfinal String DEFAULT_REDIRECT_URI = ""; 

    这里也要重新设置成你之前重定义的地址 



    具体可参考新浪微博的例子 



    自己封装了一个调用新浪微博的类  SinaWeiboUtil 



    public class SinaWeiboUtil { 



        private SinaWeiboRequestListener mWeiboRequestListener; 



        private TestActivity mActivity; 



        private static SinaWeiboUtil instance; 

       

        public static SinaWeiboUtil getInstance() { 

           return instance; 

        } 

       

        public static void createInstance(TestActivity activity) { 

           instance = new SinaWeiboUtil(activity); 

        } 

       

        private SinaWeiboUtil(TestActivity activity) { 

           mActivity = activity; 

        } 

       

        public void sendSinaWeibo() { 

           Weibo weibo = Weibo.getInstance(); 

           if (AppConst.WACCESSTOKEN != null) { 

               weibo.setRedirectUrl(Weibo.DEFAULT_REDIRECT_URI);// 此处回调页内容应该替换为与appkey对应的应用回调页 

               AccessToken accessToken = new AccessToken(AppConst.WACCESSTOKEN, 

                      Weibo.getAppSecret()); 

               weibo.setAccessToken(accessToken); 

               accessToken.setExpiresIn("86400"); 

               try { 

                  Log.v("tag==========", "secret =" + Weibo.getAppSecret() 

                         + " token =" + Weibo.getAppKey()); 

                  update(weibo, Weibo.getAppKey(), mActivity 

                         .getResources().getString(R.string.smscontent), "", ""); 

               } catch (MalformedURLException e) { 

                  // TODO Auto-generatedcatch block 

                  e.printStackTrace(); 

               } catch (NotFoundException e) { 

                  // TODO Auto-generatedcatch block 

                  e.printStackTrace(); 

               } catch (IOException e) { 

                  // TODO Auto-generatedcatch block 

                  e.printStackTrace(); 

               } catch (WeiboException e) { 

                  // TODO Auto-generatedcatch block 

                  e.printStackTrace(); 

               } 

           } else { 

              

               // Oauth2.0 

               // 隐式授权认证方式 

               weibo.setRedirectUrl(Weibo.DEFAULT_REDIRECT_URI);// 此处回调页内容应该替换为与appkey对应的应用回调页 

               // 对应的应用回调页可在开发者登陆新浪微博开发平台之后, 

               // 进入我的应用--应用详情--应用信息--高级信息--授权设置--应用回调页进行设置和查看, 

               // 应用回调页不可为空 

               weibo.authorize(mActivity, 

                      new SinaAuthDialogListener(mActivity)); 

           } 

        } 



        public void update(Weibo weibo, String source, String status, 

               String lon, String lat) throws MalformedURLException,IOException, 

               WeiboException { 

           WeiboParameters bundle = new WeiboParameters(); 

           bundle.add("source", source); 

           bundle.add("status", status); 

           if (!TextUtils.isEmpty(lon)) { 

               bundle.add("lon", lon); 

           } 

           if (!TextUtils.isEmpty(lat)) { 

               bundle.add("lat", lat); 

           } 

           String url = Weibo.SERVER + "statuses/update.json"; 

           AsyncWeiboRunner weiboRunner = new AsyncWeiboRunner(weibo); 

           if (mWeiboRequestListener == null) { 

               mWeiboRequestListener = new SinaWeiboRequestListener(mActivity); 

           } 

           weiboRunner.request(mActivity, url, bundle, Utility.HTTPMETHOD_POST, 

                  mWeiboRequestListener); 

        } 





    新浪微博认证对话框接口 

    SinaAuthDialogListener 

    public class SinaAuthDialogListenerimplements WeiboDialogListener { 



        TestActivity mActivity; 

       

        public SinaAuthDialogListener(TestActivity activity) { 

           mActivity = activity; 

        } 

       

        @Override 

        public void onComplete(Bundle values) { 

           String token = values.getString("access_token"); 

           String expires_in = values.getString("expires_in"); 

           mActivity.setWAccessToken(token); 

           Weibo.getInstance(); 

           AccessToken accessToken = new AccessToken(token, 

                  Weibo.getAppSecret()); 

           accessToken.setExpiresIn(expires_in); 

           Weibo.getInstance().setAccessToken(accessToken); 

           try { 

               SinaWeiboUtil.getInstance().update(Weibo.getInstance(),Weibo.getAppKey(), mActivity 

                      .getResources().getString(R.string.smscontent), "", ""); 

           } catch (MalformedURLException e) { 

               // TODO Auto-generatedcatch block 

               e.printStackTrace(); 

           } catch (NotFoundException e) { 

               // TODO Auto-generatedcatch block 

               e.printStackTrace(); 

           } catch (IOException e) { 

               // TODO Auto-generatedcatch block 

               e.printStackTrace(); 

           } catch (WeiboException e) { 

               // TODO Auto-generatedcatch block 

               e.printStackTrace(); 

           } 

        } 



        @Override 

        public void onError(DialogError e) { 

           Toast.makeText(mActivity, "Autherror : " + e.getMessage(), 

                  Toast.LENGTH_LONG).show(); 

        } 



        @Override 

        public void onCancel() { 

           Toast.makeText(mActivity, "Authcancel", Toast.LENGTH_LONG).show(); 

        } 



        @Override 

        public void onWeiboException(WeiboException e) { 

           Toast.makeText(mActivity, "Authexception : " + e.getMessage(), 

                  Toast.LENGTH_LONG).show(); 

        } 







    新浪微博请求接口 SinaWeiboRequestListener 

    public class SinaWeiboRequestListenerimplements RequestListener { 



        private TestActivity mActivity; 

       

        public SinaWeiboRequestListener(TestActivity activity) { 

           mActivity = activity; 

        } 

       

        @Override 

        public void onComplete(String response) { 

           mActivity.runOnUiThread(new Runnable() { 



               @Override 

               public void run() { 

                  Toast.makeText(mActivity, R.string.send_sucess, 

                         Toast.LENGTH_LONG).show(); 

               } 

           }); 

        } 



        @Override 

        public void onIOException(IOException e) { 

           // TODO Auto-generatedmethod stub 



        } 



        @Override 

        public void onError(final WeiboException e) { 

           mActivity.runOnUiThread(new Runnable() { 



               @Override 

               public void run() { 

                  Toast.makeText( 

                         mActivity, 

                         String.format( 

                                mActivity.getString(R.string.send_failed) 

                                       + ":%s", e.getMessage()), 

                         Toast.LENGTH_LONG).show(); 

               } 

           }); 



        } 







    需要在MainFest中加入以下权限 

        <uses-permission android:name="android.permission.INTERNET"> 

        </uses-permission> 

        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"> 

        </uses-permission> 

        <uses-permission android:name="android.permission.WRITE_APN_SETTINGS"> 

        </uses-permission> 

        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"> 

        </uses-permission> 

        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"> 

  • 相关阅读:
    python3编写网络爬虫18-代理池的维护
    python3编写网络爬虫17-验证码识别
    python3编写网络爬虫16-使用selenium 爬取淘宝商品信息
    python3编写网络爬虫15-Splash的使用
    python3编写网络爬虫14-动态渲染页面爬取
    LeetCode959 由斜杠划分区域(Java并查集)
    编译原理--语法分析之LR分析法的简单实现
    VsCode背景图片设置
    编译原理--基于Lex的词法分析器实验
    HDFS常用的shell命令
  • 原文地址:https://www.cnblogs.com/firecode/p/2682325.html
Copyright © 2020-2023  润新知