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);
    }

 

Android 微信登陆

上一篇:安卓版微信开启测试 接入微信运动功能


下一篇:android必学的两个项目,android仿京东、android仿微信项目(后期持续更新)