参考:http://blog.csdn.net/syz8742874/article/details/39271117
http://blog.csdn.net/woblog/article/details/51095087
Android微信SDK实现分享 里面有写好的manager可以直接使用 ,感谢博主分享 ,但里面有个要注意的地方就是博主为了分享内容的复用导致如果你想分享不同内容的时候出现分享的还是上一个的分享,内容具体请看下面的我写的微信分享的代码红字注释
常见问题 :
1.QQ空间分享打不开不报错,但就是打不开页面
注意:有可能你写成了是这段代码
private void shareToQzone () { //分享类型 params.putString(QzoneShare.SHARE_TO_QQ_KEY_TYPE,SHARE_TO_QZONE_TYPE_IMAGE_TEXT ); params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "标题");//必填 params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "摘要");//选填 params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "跳转URL");//必填 params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL, "图片链接ArrayList"); mTencent.shareToQzone(activity, params, new BaseUiListener()); }
你注意这里的params.putString(QzoneShare.SHARE_TO_QQ_IMAGE_URL, "图片链接ArrayList");
这里是错误的,因为要写成这样
ArrayList<String> imageUrls = new ArrayList<String>();
imageUrls.add("https://pic.cnblogs.com/avatar/677455/20150707171133.png");
params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrls);
后面是个集合不是一个String
应该是这样写代码
/** * 分享到QQ空间 */ public static void shareToQzone(Context context,IUiListener listener) { //分享类型 final Bundle params = new Bundle(); params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT); params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "标题 ");//必填 params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "摘要 ");//选填 params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "http://www.qq.com/news/1.html");//必填 ArrayList<String> imageUrls = new ArrayList<String>(); imageUrls.add("https://pic.cnblogs.com/avatar/677455/20150707171133.png"); params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrls); mTencent.shareToQzone((Activity)context, params, listener); }
传的参是 list 而且params.putStringArrayList() 而不是params.puString();
QQ分享本地和网络图片跟下面的红色标注有关 QQShare开头
/** * QQ分享内容 * 分享的是本地图片 SHARE_TO_QQ_IMAGE_LOCAL_URL * 分享的是网络图片 SHARE_TO_QQ_IMAGE_URL * @param context * @param listener */ public static void shareText(Context context, BeanShareInfo info,IUiListener listener) { final Bundle params = new Bundle(); params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE,QQShare.SHARE_TO_QQ_TYPE_DEFAULT); params.putString(QQShare.SHARE_TO_QQ_TITLE, info.getTitle()); params.putString(QQShare.SHARE_TO_QQ_SUMMARY, info.getDescription()); params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, info.getTargetUrl()); params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, info.getShareCoverUrl()); params.putString(QQShare.SHARE_TO_QQ_APP_NAME, info.getAppName()); params.getInt(QQShare.SHARE_TO_QQ_EXT_INT); mTencent.shareToQQ((Activity)context, params, listener); }
QQ空间分享也是 但注意的是QQ空间是 QzoneShare 开头
/** * 分享到QQ空间 * 分享的是本地图片 SHARE_TO_QQ_IMAGE_LOCAL_URL * 分享的是网络图片 SHARE_TO_QQ_IMAGE_URL */ public static void shareToQzone(Context context, BeanShareInfo info, IUiListener listener) { //分享类型 final Bundle params = new Bundle(); params.putInt(QzoneShare.SHARE_TO_QZONE_KEY_TYPE,QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT); params.putString(QzoneShare.SHARE_TO_QQ_TITLE,info.getTitle());//必填 params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, info.getDescription());//选填 params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, info.getTargetUrl());//必填 ArrayList<String> imageUrls = new ArrayList<String>(); imageUrls.add(info.getShareCoverUrl()); params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL,imageUrls); mTencent.shareToQzone((Activity)context, params, listener); }
微信常见问题:
android微信分享中bmpToByteArray报错的解决方案
1.微信分享网络图片 多看看官方demo里面有详细的用法 ,这里加线程是为了不在主线程中加载网络图片。(不然会报异常)
/* * 分享链接网络图片链接 */ private void shareWebPage(final ShareContent shareContent, final int shareType) { new Thread(new Runnable() { @Override public void run() { try { WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); // Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); Bitmap bmp = BitmapFactory.decodeStream(new URL(shareContent.getPictureUrl()).openStream()); Bitmap thumb = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true); bmp.recycle(); if (thumb == null) { Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show(); } else { msg.thumbData = Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); }catch (Exception e){ e.printStackTrace(); } } }).start(); }
2.集成微信登录和微信分享的返回码的地方区分。
// 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法 @Override public void onResp(BaseResp resp) { String result = ""; if (resp != null) { resp = resp; } switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK:
// 这里可以使用 官方API调用 resp.getType() 获取是登录后返回还是分享后返回 登录返回为1 分享返回为2 if(resp.getType() == WE_LOGIN) { result = "登录成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); String code = ((SendAuth.Resp) resp).code; mIWxData.getCode(code); }else if(resp.getType() == WE_SHARE){ result = "分享成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } finish(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "取消"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); LoadDialog.dismiss(this); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "登录被拒绝"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; default: result = "取消登录"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; } }
3.分享时出现hit push hold
解决办法是:原因是你分享的微信账号在其他手机登陆了,但是本机还没有退出登陆,导致没有权限分享。重新进入微信并登陆就好了。
示例集成微信:
注意:关于放入SDK库文件我就不说了,下面是放入后的代码说明
1.在android 的Application 中注册微信 (注意我只是展示了application中关于微信的代码 ,不代表里面就这些代码,当然还有其他的代码)
/** * 微信API */ public static IWXAPI api; /** * 初始化微信 */ private void registWX() { //通过WXAPIFactory工厂 api = WXAPIFactory.createWXAPI(this,WX_APP_ID,false); api.registerApp(WX_APP_ID); }
2.先集成微信分享 可以编写微信管理类,是参考的上面博客中的内容,直接拿来用然后自己按自己需求改了改的
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.widget.Toast; import com.tencent.mm.sdk.modelmsg.SendMessageToWX; import com.tencent.mm.sdk.modelmsg.WXImageObject; import com.tencent.mm.sdk.modelmsg.WXMediaMessage; import com.tencent.mm.sdk.modelmsg.WXTextObject; import com.tencent.mm.sdk.modelmsg.WXVideoObject; import com.tencent.mm.sdk.modelmsg.WXWebpageObject; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.WXAPIFactory; import java.net.URL; /** * * 微信分享管理类 * */ public class WechatShareManager { private static final int THUMB_SIZE = 150; public static final int WECHAT_SHARE_WAY_TEXT = 1; //文字 public static final int WECHAT_SHARE_WAY_PICTURE = 2; //图片 public static final int WECHAT_SHARE_WAY_WEBPAGE = 3; //链接 public static final int WECHAT_SHARE_WAY_VIDEO = 4; //视频 public static final int WECHAT_SHARE_TYPE_TALK = SendMessageToWX.Req.WXSceneSession; //会话 public static final int WECHAT_SHARE_TYPE_FRENDS = SendMessageToWX.Req.WXSceneTimeline; //朋友圈 private static WechatShareManager mInstance; private ShareContent mShareContentText, mShareContentPicture, mShareContentWebpag, mShareContentVideo; private IWXAPI mWXApi; private Context mContext; private WechatShareManager(Context context){ this.mContext = context; //初始化数据 //初始化微信分享代码 initWechatShare(context); } /** * 获取WeixinShareManager实例 * 非线程安全,请在UI线程中操作 * @return */ public static WechatShareManager getInstance(Context context){ if(mInstance == null){ mInstance = new WechatShareManager(context); } return mInstance; } private void initWechatShare(Context context){ if (mWXApi == null) { // mWXApi = WXAPIFactory.createWXAPI(context, McvApplication.WX_APP_ID, true); mWXApi = McvApplication.api; //如果你在Appcation中注册了微信这里可用Appcation的api 我这里使用了 } // mWXApi.registerApp(McvApplication.WX_APP_ID); //appcation中注册过了 } /** * 通过微信分享 * @param shareContent 分享的方式(文本、图片、链接) * @param shareType 分享的类型(朋友圈,会话) */ public void shareByWebchat(ShareContent shareContent, int shareType){ if(checkInstall()){ switch (shareContent.getShareWay()) { case WECHAT_SHARE_WAY_TEXT: shareText(shareContent, shareType); break; case WECHAT_SHARE_WAY_PICTURE: sharePicture(shareContent, shareType); break; case WECHAT_SHARE_WAY_WEBPAGE: shareWebPage(shareContent, shareType); break; case WECHAT_SHARE_WAY_VIDEO: shareVideo(shareContent, shareType); break; } } } /** * 检查是否安装微信 * @return */ private boolean checkInstall() { if(mWXApi.isWXAppInstalled()){ return true; }else { Toast.makeText(mContext,"请安装微信",Toast.LENGTH_SHORT).show(); return false; } } private abstract class ShareContent { protected abstract int getShareWay(); protected abstract String getContent(); protected abstract String getTitle(); protected abstract String getURL(); protected abstract int getPictureResource(); protected abstract String getPictureUrl(); } /** * 设置分享文字的内容 * @author chengcj1 * */ public class ShareContentText extends ShareContent { private String content; /** * 构造分享文字类 * @param content 分享的文字内容 */ public ShareContentText(String content){ this.content = content; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_TEXT; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } @Override protected int getPictureResource() { return -1; } @Override protected String getPictureUrl() { return null; } } /* * 获取文本分享对象 如果你每次想要分享的需要不同的内容就别进行if判断不然会复用之前的信息 */ public ShareContent getShareContentText(String content) { // if (mShareContentText == null) { mShareContentText = new ShareContentText(content); // } return (ShareContentText) mShareContentText; } /** * 设置分享图片的内容 * @author chengcj1 * */ public class ShareContentPicture extends ShareContent { private int pictureResource; public ShareContentPicture(int pictureResource){ this.pictureResource = pictureResource; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_PICTURE; } @Override protected int getPictureResource() { return pictureResource; } @Override protected String getPictureUrl() { return null; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return null; } } /* * 获取图片分享对象 如果你每次想要分享的需要不同的内容就别进行 if 判断不然会复用之前的信息 */ public ShareContent getShareContentPicture(int pictureResource) { // if (mShareContentPicture == null) { mShareContentPicture = new ShareContentPicture(pictureResource); // } return (ShareContentPicture) mShareContentPicture; } /** * 设置分享链接的内容 * @author chengcj1 * */ public class ShareContentWebpage extends ShareContent { private String title; private String content; private String url; private int pictureResource; private String pictureUrl; public ShareContentWebpage(String title, String content, String url,String pictureUrl){ this.title = title; this.content = content; this.url = url; this.pictureUrl = pictureUrl; } public ShareContentWebpage(String title, String content, String url,int pictureResource){ this.title = title; this.content = content; this.url = url; this.pictureResource = pictureResource; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_WEBPAGE; } @Override protected String getContent() { return content; } @Override protected String getTitle() { return title; } @Override protected String getURL() { return url; } @Override protected int getPictureResource() { return pictureResource; } @Override protected String getPictureUrl() { return pictureUrl; } } /* * 获取网页分享对象 如果你每次想要分享的需要不同的内容就别进行if判断不然会复用之前的信息 */ public ShareContent getShareContentWebpag(String title, String content, String url,String pictureLocalUrl) { // if (mShareContentWebpag == null) { mShareContentWebpag = new ShareContentWebpage(title, content, url, pictureLocalUrl); // } return (ShareContentWebpage) mShareContentWebpag; } /** * 设置分享视频的内容 * @author chengcj1 * */ public class ShareContentVideo extends ShareContent { private String url; public ShareContentVideo(String url) { this.url = url; } @Override protected int getShareWay() { return WECHAT_SHARE_WAY_VIDEO; } @Override protected String getContent() { return null; } @Override protected String getTitle() { return null; } @Override protected String getURL() { return url; } @Override protected int getPictureResource() { return -1; } @Override protected String getPictureUrl() { return null; } } /* * 获取视频分享内容 如果你每次想要分享的需要不同的内容就别进行if判断不然会复用之前的信息 */ public ShareContent getShareContentVideo(String url) { // if (mShareContentVideo == null) { mShareContentVideo = new ShareContentVideo(url); // } return (ShareContentVideo) mShareContentVideo; } /* * 分享文字 */ private void shareText(ShareContent shareContent, int shareType) { String text = shareContent.getContent(); //初始化一个WXTextObject对象 WXTextObject textObj = new WXTextObject(); textObj.text = text; //用WXTextObject对象初始化一个WXMediaMessage对象 WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = textObj; msg.description = text; //构造一个Req SendMessageToWX.Req req = new SendMessageToWX.Req(); //transaction字段用于唯一标识一个请求 req.transaction = buildTransaction("textshare"); req.message = msg; //发送的目标场景, 可以选择发送到会话 WXSceneSession 或者朋友圈 WXSceneTimeline。 默认发送到会话。 req.scene = shareType; mWXApi.sendReq(req); } /* * 分享图片 */ private void sharePicture(ShareContent shareContent, int shareType) { Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); WXImageObject imgObj = new WXImageObject(bitmap); WXMediaMessage msg = new WXMediaMessage(); msg.mediaObject = imgObj; Bitmap thumbBitmap = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true); bitmap.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); //设置缩略图 SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("imgshareappdata"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } /* * 分享链接,因为我这里设置的图片是网络图片(http://开头),因为android是不能在主线程中调用请求网络的耗时操作的,所以加了线程 */ private void shareWebPage(final ShareContent shareContent, final int shareType) { new Thread(new Runnable() { @Override public void run() { try { WXWebpageObject webpage = new WXWebpageObject(); webpage.webpageUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(webpage); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); // Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), shareContent.getPictureResource()); //这里调用的是本工程下的资源文件下的图片,如果是本地图片是不用加线程的,如:R.drawable.ic_languner Bitmap bmp = BitmapFactory.decodeStream(new URL(shareContent.getPictureUrl() + "?x-oss-process=image/resize,m_fixed,h_60,w_60").openStream()); //这里是看官方dome中分享网络图片后改的 Bitmap thumb = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true); bmp.recycle(); if (thumb == null) { Toast.makeText(mContext, "图片不能为空", Toast.LENGTH_SHORT).show(); } else { msg.thumbData = Util.bmpToByteArray(thumb, true); } SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("webpage"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); }catch (Exception e){ e.printStackTrace(); } } }).start(); } /* * 分享视频 */ private void shareVideo(ShareContent shareContent, int shareType) { WXVideoObject video = new WXVideoObject(); video.videoUrl = shareContent.getURL(); WXMediaMessage msg = new WXMediaMessage(video); msg.title = shareContent.getTitle(); msg.description = shareContent.getContent(); Bitmap thumb = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher); // BitmapFactory.decodeStream(new URL(video.videoUrl).openStream()); /** * 测试过程中会出现这种情况,会有个别手机会出现调不起微信客户端的情况。造成这种情况的原因是微信对缩略图的大小、title、description等参数的大小做了限制,所以有可能是大小超过了默认的范围。 * 一般情况下缩略图超出比较常见。Title、description都是文本,一般不会超过。 */ Bitmap thumbBitmap = Bitmap.createScaledBitmap(thumb, THUMB_SIZE, THUMB_SIZE, true); thumb.recycle(); msg.thumbData = Util.bmpToByteArray(thumbBitmap, true); SendMessageToWX.Req req = new SendMessageToWX.Req(); req.transaction = buildTransaction("video"); req.message = msg; req.scene = shareType; mWXApi.sendReq(req); } private String buildTransaction(final String type) { return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis(); } }
3.微信分享的使用
private WechatShareManager mShareManager; /** * 微信分享 按照个人需求更改 我这里是自定义了一个model类来传分享的内容,而且这里只用到了分享链接 * @param isFriend 是否是朋友圈的分享 */ private void WChatShare(boolean isFriend){ BeanShareInfo shareInfo = (BeanShareInfo) this.info; WechatShareManager.ShareContentWebpage shareWebpage = (WechatShareManager.ShareContentWebpage) mShareManager.getShareContentWebpag(shareInfo.getTitle(),shareInfo.getDescription(),shareInfo.getTargetUrl(),shareInfo.getShareCoverUrl()); if(isFriend){ //朋友圈 mShareManager.shareByWebchat(shareWebpage,WechatShareManager.WECHAT_SHARE_TYPE_FRENDS); }else{ mShareManager.shareByWebchat(shareWebpage,WechatShareManager.WECHAT_SHARE_TYPE_TALK); } }
4.集成微信登录
首先做的还是上文第1点的内容
然后开始代码
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.JsonHttpResponseHandler; import com.tencent.mm.sdk.modelbase.BaseReq; import com.tencent.mm.sdk.modelbase.BaseResp; import com.tencent.mm.sdk.modelmsg.SendAuth; import com.tencent.mm.sdk.openapi.IWXAPI; import com.tencent.mm.sdk.openapi.IWXAPIEventHandler; import com.uethinking.microvideo.application.McvApplication; import com.uethinking.microvideo.view.LoadDialog; import org.apache.http.Header; import java.net.URLEncoder; public class WXEntryActivity extends Activity implements IWXAPIEventHandler { /** * 登录类型码为 1 */ public static final int WE_LOGIN = 1; /** * 分享类型码为 0 */ public static final int WE_SHARE = 2; private static IWxData mIWxData; public static void setIWxData(IWxData interfaces){ mIWxData = interfaces; } //自己写的回调接口 public interface IWxData{ /** * 获取code */ void getCode(String code); } /** APPID **/ public String WX_APP_ID = McvApplication.WX_APP_ID; // IWXAPI 是第三方app和微信通信的openapi接口 private IWXAPI api; private BaseResp resp = null; // 获取第一步的code后,请求以下链接获取access_token private String GetCodeRequest = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code"; // 获取用户个人信息 private String GetUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID"; private String WX_APP_SECRET = "创建应用后得到的APP_SECRET"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // api = WXAPIFactory.createWXAPI(this, WX_APP_ID, false); api = McvApplication.api; api.handleIntent(getIntent(), this); } // 微信发送请求到第三方应用时,会回调到该方法 @Override public void onReq(BaseReq req) { finish(); } // 第三方应用发送到微信的请求处理后的响应结果,会回调到该方法 @Override public void onResp(BaseResp resp) { String result = ""; if (resp != null) { resp = resp; } switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: if(resp.getType() == WE_LOGIN) { result = "登录成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); String code = ((SendAuth.Resp) resp).code; // 因为我只是获取wechatCode 然后通过接口 将code传到我们后台他去完成后台获取用户信息然后注册返回是否成功给我,所以我就没有用下面的getUserInfo的方法,但下面的可以用 mIWxData.getCode(code); // /* // * 将你前面得到的AppID、AppSecret、code,拼接成URL 获取access_token等等的信息(微信) // */ // String get_access_token = getCodeRequest(code); // AsyncHttpClient client = new AsyncHttpClient(); // client.post(get_access_token, new JsonHttpResponseHandler() { // @Override // public void onSuccess(int statusCode, JSONObject response) { // // TODO Auto-generated method stub // super.onSuccess(statusCode, response); // try { // // // if (!response.equals("")) { // String access_token = response // .getString("access_token"); // String openid = response.getString("openid"); // String get_user_info_url = getUserInfo( // access_token, openid); // getUserInfo(get_user_info_url); // } // // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } // }); }else if(resp.getType() == WE_SHARE){ result = "分享成功"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } finish(); break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "取消"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); LoadDialog.dismiss(this); finish(); break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "登录被拒绝"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; default: result = "取消登录"; Toast.makeText(this, result, Toast.LENGTH_LONG).show(); finish(); break; } } /** * 通过拼接的用户信息url获取用户信息 * * @param user_info_url */ private void getUserInfo(String user_info_url) { AsyncHttpClient client = new AsyncHttpClient(); client.get(user_info_url, new JsonHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { super.onSuccess(statusCode, headers, responseString); } // @Override // public void onSuccess(int statusCode, JSONObject response) { // // TODO Auto-generated method stub // super.onSuccess(statusCode, response); // try { // // System.out.println("获取用户信息:" + response); // // if (!response.equals("")) { // String openid = response.getString("openid"); // String nickname = response.getString("nickname"); // String headimgurl = response.getString("headimgurl"); // // } // // } catch (Exception e) { // // TODO Auto-generated catch block // e.printStackTrace(); // } // } }); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); setIntent(intent); api.handleIntent(intent, this); finish(); } /** * 获取access_token的URL(微信) * * @param code * 授权时,微信回调给的 * @return URL */ private String getCodeRequest(String code) { String result = null; GetCodeRequest = GetCodeRequest.replace("APPID", urlEnodeUTF8(WX_APP_ID)); GetCodeRequest = GetCodeRequest.replace("SECRET", urlEnodeUTF8(WX_APP_SECRET)); GetCodeRequest = GetCodeRequest.replace("CODE", urlEnodeUTF8(code)); result = GetCodeRequest; return result; } /** * 获取用户个人信息的URL(微信) * * @param access_token * 获取access_token时给的 * @param openid * 获取access_token时给的 * @return URL */ private String getUserInfo(String access_token, String openid) { String result = null; GetUserInfo = GetUserInfo.replace("ACCESS_TOKEN", urlEnodeUTF8(access_token)); GetUserInfo = GetUserInfo.replace("OPENID", urlEnodeUTF8(openid)); result = GetUserInfo; return result; } private String urlEnodeUTF8(String str) { String result = str; try { result = URLEncoder.encode(str, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return result; } }
5.调用的地方
/** * 微信登录 */ private void wechatLogin() { if(!NetworkUtils.isnetWorkAvilable()){ ToastUtil.showShort(this,"暂无网络连接"); return; } // WXapi = WXAPIFactory.createWXAPI(this, WXEntryActivity.WX_APP_ID, true); // WXapi.registerApp(WXEntryActivity.WX_APP_ID); WXapi = McvApplication.api; // 判断是否安装了微信客户端 if (!WXapi.isWXAppInstalled()) { ToastUtil.showShort(this, "您还未安装微信客户端!"); return; } LoadDialog.show(this,"正在打开微信..."); //弹出框
// 上面写的用来获取wechatCode的回调接口 设置进去 WXEntryActivity.setIWxData(new WXEntryActivity.IWxData() { @Override public void getCode(String code) { //请求本地微信接口 // ToastUtil.showShort(RegisterOneActivity.this,code); mLoginManager.weChatLogin(code); } }); SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "com"; WXapi.sendReq(req); }
6.以上是我用到的,如果有帮助就好,再次做个记录,还要不免吐槽一下,微信开放平台文档是什么鬼东西,写的能看吗?