Android 微信登录

/1、首先你得到微信.开放平台申请开发权限:https://open.weixin.qq.com/

申请通过效果如下即可

Android 微信登录_第1张图片

 

2、其次,阅读开发文档:

https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317851&token=5e2b1bed1e22d52b73509b9def8c200bf130fb3f&lang=zh_CN

 

3、接下来就是Android开发代码。分三步获取微信信息,用以登录

 

3/1常量类

public class Constants {

    // appid

    public static final String APP_ID = "wx1201a018fdcec6";
    public static final String SECRET = "fc866ba851439dead6a09a480de179";

    // 商户号

    public static final String MCH_ID = "15171351";

    /**
     * 微信开放平台和商户约定的支付密钥
     *
     * 注意:不能hardcode在客户端,建议genSign这个过程由服务器端完成
     */

    public static final String APP_KEY = "01b2e2bd220db0c3765609ebc9b59f";

}

3/2起调微信登录

//微信登录

private void wxlogin(){
    String  strState = getPackageName() + String.valueOf(System.currentTimeMillis());
    String SCOPE = "snsapi_userinfo";
    IWXAPI api;
    api = WXAPIFactory.createWXAPI(LiteActivity.this, Constants.APP_ID, false);
    api.registerApp(Constants.APP_ID);

    if (!api.isWXAppInstalled()) {
       Toast.makeText(LiteActivity.this,"请先安装微信客户端!",Toast.LENGTH_LONG).show();
        return;
    }
    final SendAuth.Req req = new SendAuth.Req();
    req.scope = SCOPE;
    req.state = strState;
    api.sendReq(req);

}

3/3获取微信回调结果,微信开发必要activity

/**
 * 微信分享回调
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI wxAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        wxAPI = WXAPIFactory.createWXAPI(this, Constants.APP_ID,true);
        wxAPI.registerApp(Constants.APP_ID);
        wxAPI.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent){
        super.onNewIntent(intent);
        wxAPI.handleIntent(getIntent(),this);
        Log.i("ansen","WXEntryActivity onNewIntent");
    }

    @Override
    public void onReq(BaseReq arg0) {
        Log.i("ansen","WXEntryActivity onReq:"+arg0);
    }

    @Override
    public void onResp(BaseResp resp){
        if(resp.getType()== ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX){//分享
            Log.i("ansen","微信分享操作.....");

        }else if(resp.getType()==ConstantsAPI.COMMAND_SENDAUTH){//登陆
            SendAuth.Resp authResp = (SendAuth.Resp) resp;
            String code = authResp.code;
            TestObServernotice.getInstance().notifyObserver(111, 2, code);
            Log.i("ansen", "微信登录操作.....");

        }
        finish();
    }
}

 配置微信activity


 

3/4  resp结果如下。

Android 微信登录_第2张图片

请求微信接口

public static final String wxlogurl = "https://api.weixin.qq.com/sns/userinfo";
public static final String wxlogurl2 = "https://api.weixin.qq.com/sns/oauth2/access_token";

3/5通过观察者模式,回到activity中,根据code获取token

请求如下:,这里是asyncHttp框架。你可以使用其他请求网络类

private void getUserInfo(final String access_token, final String openid) {

    String url = Url.wxlogurl2;
    RequestParams params = new RequestParams();
    params.put("appid", openid);
    params.put("secret", Constants.SECRET);
    params.put("code", access_token);
    params.put("grant_type", "authorization_code");
    more_loadDialog.setloadtext("登录中...");
    more_loadDialog.show();
    AsyncHttpClient client = new AsyncHttpClient();

    client.post(url, params, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
            more_loadDialog.dismiss();
            Toast.makeText(LiteActivity.this, "网络连接异常", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSuccess(int i, Header[] headers, String s) {
            try {
                more_loadDialog.dismiss();
                JSONObject object = new JSONObject(s);
                String at = object.getString("access_token");
                getUserInfoend(at,Constants.APP_ID);
                Log.i("lgq", "re==logtest=====" + s);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}

结果如下

Android 微信登录_第3张图片

3/6根据token获取微信账号详情

请求如下

private void getUserInfoend(final String access_token, final String openid) {

    String url = Url.wxlogurl;
    RequestParams params = new RequestParams();
    params.put("openid", openid);
    params.put("access_token", access_token);
    more_loadDialog.setloadtext("登录中...");
    more_loadDialog.show();
    AsyncHttpClient client = new AsyncHttpClient();

    client.post(url, params, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
            more_loadDialog.dismiss();
            Toast.makeText(LiteActivity.this, "网络连接异常", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSuccess(int i, Header[] headers, String s) {
            try {
                more_loadDialog.dismiss();
                JSONObject object = new JSONObject(s);
                Log.i("lgq", "re==logtest=====" + s);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });
}

结果如下:

Android 微信登录_第4张图片

4、所有微信信息已经请求得到了。接下来就是。需要什么参数登录,就使用什么参数登录了 !!!!!!

 

观察者模式类TestObServernotice:https://blog.csdn.net/meixi_android/article/details/81016359

 

在线回复解决bug:qq1085220040

 

 

 

你可能感兴趣的:(移动开发)