Android 微信登陆

1.防坑指南:

  1.  微信未返回Code?

    1.  请更新libammsdk.jar,import com.tencent.mm.sdk.modelmsg.SendAuth。发现命名空间发生了很大的变化。(微信婊这么大的改动也不说下,这么查的 啊,日了狗了。)
  2. 如何接收微信授权后的回调?
    1.  在包名下建立wxapi目录,并在该wxapi目录下新增一个WXEntryActivity类
  3. 为什么回调没有执行?
    1.   配置AndroidManifest,如下:(此问题我也是参考别人的出来的,不知道是不是这个原因。)
<activity
            android:name="net.sourceforge.simcpux.wxapi.WXEntryActivity"
            android:configChanges="orientation|keyboardHidden"
            android:exported="true"
            android:launchMode="singleTop"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" >
                </action>

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
          1. 4  如何拿到Code
private void handleIntent(Intent intent) {
            String result = "";
            ConUrls.WriteLog("handleIntent"+result);
            SendAuth.Resp resp = new SendAuth.Resp(intent.getExtras());
            if (resp.errCode == BaseResp.ErrCode.ERR_OK) {
                //用户同意
                result ="发送成功";
                //ConUrls.WriteLog("handleIntent"+result);
                //Contexts.CurWeiXinHelper;
                String    weixinCode = resp.code;
                ConUrls.WriteLog("onResume "+weixinCode);
                if(Contexts.CurWeiXinHelper !=null)
                {
                    ConUrls.WriteLog("CurWeiXinHelper "+weixinCode);
                    try
                    {

                        Contexts.CurWeiXinHelper.GetLoginInfo(weixinCode);
                        Contexts.CurWeiXinHelper=null;
                        finish();
                    }
                    catch(Exception e)
                    {
                        ConUrls.WriteLog(e.getMessage());
                    }

                }
            }
        }

5.如何根据code  拿到 openId?

  

private  void WXGetAccessToken(){
        HttpClient get_access_token_httpClient = new DefaultHttpClient();
        HttpClient get_user_info_httpClient = new DefaultHttpClient();
        String access_token="";
        String openid ="";
        try {
            HttpPost postMethod = new HttpPost(get_access_token);
            HttpResponse response = get_access_token_httpClient.execute(postMethod); // 执行POST方法
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                InputStream is = response.getEntity().getContent();
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(is));
                String str = "";
                StringBuffer sb = new StringBuffer();
                while ((str = br.readLine()) != null) {
                    sb.append(str);
                }
                is.close();
                String josn = sb.toString();
                JSONObject json1 = new JSONObject(josn);
                access_token = (String) json1.get("access_token");
                openid = (String) json1.get("openid");

                WeiXinAuthLoginModel model=new WeiXinAuthLoginModel();
                model.setOpenId(openid);
                LoginModel MyLoginModel= SysHelper.GetWeiXinLogin(model, curContent);
                Contexts.MyLoginModel = MyLoginModel;
                SysHelper.StartAm(curContent);
                if(mHandler!=null)
                {
                    Message msg=new Message();
                    msg.obj=MyLoginModel;
                    msg.arg1=mHandlerOrg1;
                    msg.what=1;
                    mHandler.sendMessage(msg);
                }

            } else {
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            if(mHandler!=null)
            {
                Message msg=new Message();
                msg.obj="登录失败";
                msg.arg1=mHandlerOrg1;
                msg.arg2=-1;
                msg.what=0;
                mHandler.sendMessage(msg);
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            if(mHandler!=null)
            {
                Message msg=new Message();
                msg.obj="登录失败";
                msg.arg1=mHandlerOrg1;
                msg.arg2=-1;
                msg.what=0;
                mHandler.sendMessage(msg);
            }
        } catch (IOException e) {
            e.printStackTrace();
            if(mHandler!=null)
            {
                Message msg=new Message();
                msg.obj="登录失败";
                msg.arg1=mHandlerOrg1;
                msg.arg2=-1;
                msg.what=0;
                mHandler.sendMessage(msg);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            if(mHandler!=null)
            {
                Message msg=new Message();
                msg.obj="登录失败";
                msg.arg1=mHandlerOrg1;
                msg.arg2=-1;
                msg.what=0;
                mHandler.sendMessage(msg);
            }
        }
        //不获取 info
//        String get_user_info_url=getUserInfo(access_token,openid);
//        WXGetUserInfo(get_user_info_url);
    }
时间: 2024-08-02 07:56:47

Android 微信登陆的相关文章

Android微信登陆

前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下. 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 一.准备 注册.认证步骤自己来.微信开放平台:https://open.weixin.qq.com 二. 代码 2.1 唤起微信登录 参考官网文档:资源中心/移动应用开放/接入指南/Android接入指南 private IW

Android调用微信登陆、分享、支付

前言:用了微信sdk各种痛苦,感觉比qq sdk调用麻烦多了,回调过于麻烦,还必须要在指定包名下的actvity进行回调,所以我在这里写一篇博客,有这个需求的朋友可以借鉴一下,以后自己别的项目有用到也有个找资料的地方. 一.微信登陆分三个步骤: 1).微信授权登陆  2).根据授权登陆code 获取该用户token  3).根据token获取用户资料  4).接收微信的请求及返回值 如果你的程序需要接收微信发送的请求,或者接收发送到微信请求的响应结果,需要下面3步操作: a. 在你的包名相应目录

Android开发中微信登陆

关于android开发中的微信登陆,除了使用shareSDK,我们使用腾讯开发者平台上提供的SDK,按照官网步骤实现第三方登陆的功能. 微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权.该模式整体流程为: 1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数: 2. 通过code参数加上AppID和AppSecret等,通过API换取access_tok

android app 微信登陆

目前第三方登陆app需求很多了.在这里做一下微信app的登陆.直说其中几个功能点. https://open.weixin.qq.com,首先去微信官网去注册app,然后下载demo.获取libammsdk.jar. 在进行第三方登陆的页面进行如下的初始化: // Constants.APP_ID 是在官网注册app时,官网分配的appid api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, true); api.registerApp(

微信登陆,微信SDK授权登陆经验分享

From:http://www.eoeandroid.com/thread-547012-1-1.html 最近因为项目需要做了微信登陆,好像也是微信最近才放出来的接口.还需要申请才能有权限实现授权.其实也比较简单,跟新浪微博和qq授权登陆差不多.不过还是有点差别,不知道是微信sdk本身就没有完善还是其他问题.会有一点蛋疼的地方,下面会细说.(由于等级不够,不能发在经验分享区,只能先写在这里了.希望对要做微信登陆的朋友有帮助,如果有不对的地方还希望大家直言不讳)<ignore_js_op> 首

转-Android微信支付

http://blog.fangjie.info/android微信支付/ Android微信支付 2014-08-09 一.使用微信官方的提供的demo里的appid等 1.微信接口上手指南:(从“移动应用开发”->“Android接入指南”)建议,先把官方这篇文章看完 2.微信支付接口(从“移动应用开发”->“Android开发手册” )(下载官方demohttps://res.wx.qq.com/paymchres/zh_CN/htmledition/download/wxpay/app

【已解决】Android微信开放平台,申请移动应用的 应用签名 如何获取

你看到的这个文章来自于http://www.cnblogs.com/ayanmw 在微信开放平台,申请移动应用的时候: https://open.weixin.qq.com/cgi-bin/appcreate?t=manage/createMobile&type=app&lang=zh_CN&token=60682ddfbd9106b1c4b1f9d70f56c98e5f728905 下一步后需要填写应用签名 这可难倒了我了..签名 keystore文件可没有这么简单. "

Android简单登陆页面

布局: 线性布局+相对布局 日志打印: 利用LogCat和System.out.println打印观察. Onclick事件是采用过的第四种: 在配置文件中给Button添加点击时间 涉及知识: 通过上线文context获得文件的路径和缓存路径,保存文件 布局代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.an

andorid 练习微信登陆

AndroidManifest.xml layout1.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height=&quo