Android腾讯微博一键分享

最近在做将android应用分享到微博这个功能。下面我将贴出关键代码希望能帮助到给位朋友(这一篇将贴出腾讯微博分享的关键代码)

一、“分享到腾讯微博”按钮点击时的执行代码

//设置回调
                oAuth = new OAuthV1("null");
                // 开发者的key,开发者的seceret
                oAuth.setOauthConsumerKey(oauthConsumeKey);//自己在腾讯微博注册的应用对应的key和sina差不多这个懂了其它这个就不是问题了。
                oAuth.setOauthConsumerSecret(oauthConsumerSecret);
                try {
                    // 获取token
                    oAuth = OAuthV1Client.requestToken(oAuth);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                // 跳转到微博的鉴权页面
                Intent tengxun_intent = new Intent(ShareActivity.this,
                        OAuthV1AuthorizeWebView.class);
                tengxun_intent.putExtra("oauth", oAuth);
                // 以返回值的方式启动
                startActivityForResult(tengxun_intent, 100);

点击后的效果图:

Android腾讯微博一键分享

使用qq号登陆,然后点击授权,成功授权后弹出dialog,供用户输入自定义信息

二、在onActivityResult方法中得到授权信息

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // 鉴权成功后返回到这个Activity,并且一些信息和操作
                if (requestCode == 100) {

                    if (resultCode == OAuthV1AuthorizeWebView.RESULT_CODE) {
                        // 取得返回的OAuthV1类实例oAuth
                        oAuth = (OAuthV1) data.getExtras().getSerializable("oauth");
                        try {
                            // 通过调用accessToken方法,向腾讯微博开放平台请求获得未授权的access_token和access_token_secret,请在具体方法中添加
                            oAuth = OAuthV1Client.accessToken(oAuth);
                        } catch (Exception e1) {
                            e1.printStackTrace();
                        }


                        // 获取用户的个人信息
                        UserAPI userAPI = new UserAPI(OAuthConstants.OAUTH_VERSION_1);
                        try {
                            String response = userAPI.info(oAuth, "json");
                            Log.e("userInfo", response);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        userAPI.shutdownConnection();
                        dialog.show();
                    }
                }
                }

下面贴出dialog类

package cn.mlpark.client.util;

import cn.mlpark.client.MistakeChoicePointActivity;
import cn.mlpark.client.R;
import cn.mlpark.client.ShareActivity;
import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ShareDialog extends Dialog{
    private Button btn_cancel;
    private Button btn_send;
    private TextView tv_title;
    private EditText et_content;
    private String content;
    private Context context;
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public ShareDialog(Context context, int theme) {
        super(context, theme);
        setContentView(R.layout.share_dialog);
        this.context = context;
        btn_cancel = (Button)findViewById(R.id.btnClose);
        btn_send = (Button)findViewById(R.id.btnSend);
        btn_cancel.setOnClickListener(new ShareDialogOnClickListener());
        btn_send.setOnClickListener(new ShareDialogOnClickListener());
        tv_title = (TextView)findViewById(R.id.share_dialog_tv_title);
        et_content = (EditText)findViewById(R.id.etEdit);
    }
    
    class ShareDialogOnClickListener implements android.view.View.OnClickListener{
        public void onClick(View v) {
            switch(v.getId()){
            case R.id.btnClose:
                dismiss();
                break;
                
            case R.id.btnSend:
                setContent(et_content.getText().toString());//自定义信息
                if (context instanceof ShareActivity) {
                    ((ShareActivity) context).handler.sendEmptyMessage(Constants.SHARE_TENGXUN);
                }
                dismiss();
                break;
            }
        }
        
    }
}

Android腾讯微博一键分享

然后点击发送按钮将执行分享操作,分享操作的代码为:

TAPI tAPI= new TAPI(OAuthConstants.OAUTH_VERSION_1);
                    try {
                        File fileDir=new File("/sdcard/mlparkqweibosdk2");
                        if(!fileDir.exists()) fileDir.mkdirs();
                        File file=new File("/sdcard/mlparkqweibosdk2/ml_weibo_icon.jpg");
                        if(!file.exists()){
                            file.createNewFile();
                            InputStream inputStream=ShareActivity.class.getResourceAsStream("/res/drawable-hdpi/ml_weibo_icon.jpg");
                            FileOutputStream fileOutputStream=new FileOutputStream(file);
                            byte[] buf=new byte[1024];
                            int ins;
                            while ((ins=inputStream.read(buf))!=-1) {
                                fileOutputStream.write(buf,0,ins);
                            }
                            inputStream.close();
                            fileOutputStream.close();
                        }
                        String picPath="/sdcard/mlparkqweibosdk2/ml_weibo_icon.jpg";
                        String response=tAPI.addPic(oAuth,  "json",  dialog.getContent()+":\t"+weiboContent, "127.0.0.1", picPath);
                        JSONObject json = new JSONObject(response);
                        int errorcode = json.optInt("errcode");
                        if (errorcode == 0) {
                            Toast.makeText(getApplicationContext(), "分享成功",
                                    1000).show();
                        } else {
                            Toast.makeText(getApplicationContext(),
                                    "分享失败:", 1000).show();
                        }
                        finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    tAPI.shutdownConnection();

然后就分享成功了。

 

转载于:https://www.cnblogs.com/tony-yang-flutter/archive/2013/03/18/android%e8%85%be%e8%ae%af%e5%be%ae%e5%8d%9a%e4%b8%80%e9%94%ae%e5%88%86%e4%ba%ab.html