实现Android移动端的登录

论坛 期权论坛 脚本     
匿名网站用户   2020-12-19 20:21   30   0

第一步,当然是去微信开放平台去申请到一个APP_ID和APP_SECRET,这一步和其他的第三方是一样的,不多说。

第二步,下载微信提供的SDK,将lib包下的jar文件libammsdk.jar拷到自己的lib底下。

第三步,在自己项目的activity实现微信授权登录的方法下,将项目注册到微信

api = WXAPIFactory.createWXAPI(this,Constants.WX_LOGIN_APP_ID,true);
api.registerApp(Constants.WX_LOGIN_APP_ID);

SendAuth.Req req = new SendAuth.Req();  
req.scope = "snsapi_userinfo";  
req.state = "wechat_sdk_demo_test";  
api.sendReq(req); 

第四步,在自己项目包目录下建一个子包wxapi,在其下面创建一个类WXEntryActivity,实现与微信服务器端的交互(我这里是用XUtils的HttpUtils方法实现的服务器数据访问)

public class WXEntryActivity extends Activity implements IWXAPIEventHandler{
    private HttpUtils hUtils = new HttpUtils();
    private IWXAPI api;
    private String code;
    private String access_token;
    private String openid;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
     api = WXAPIFactory.createWXAPI(this,Constants.WX_LOGIN_APP_ID, false);
     api.registerApp(Constants.WX_LOGIN_APP_ID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
 super.onNewIntent(intent);
 setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onResp(BaseResp resp) {
 Log.i("info", "--onResp--");
 switch (resp.errCode) {
 case BaseResp.ErrCode.ERR_OK:
  code = ((SendAuth.Resp) resp).code;
   
  String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + Constants.WX_LOGIN_APP_ID
    +"&secret=" + Constants.WX_LOGIN_APP_SECRET
    +"&code="+ code 
    +"&grant_type=authorization_code";
  hUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() {

   @Override
   public void onFailure(HttpException arg0, String arg1) {
     
   }

   @Override
   public void onSuccess(ResponseInfo<String> responseInfo) {
    String result = responseInfo.result;
    try {
     JSONObject obj = new JSONObject(result);
     access_token = obj.optString("access_token");
//     Log.i("info", "--access_token--"+access_token);
     openid = obj.optString("openid");
//     Log.i("info", "--openid--"+openid);
      
     String getInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token
       +"&openid="+openid;
     hUtils.send(HttpMethod.GET, getInfoUrl, new RequestCallBack<String>() {

      @Override
      public void onFailure(HttpException arg0, String arg1) {
      }

      @Override
      public void onSuccess(ResponseInfo<String> responseInfo) {
       String result = responseInfo.result;
       RequestParams params = new RequestParams();//向服务器提交的数据
       String requestUrl = null;//服务器返回数据接口
       hUtils.send(HttpMethod.POST,requestUrl,params,new RequestCallBack<String>() {

        @Override
        public void onFailure(HttpException arg0,
           String arg1) {
        }

        @Override
        public void onSuccess(
         ResponseInfo<String> responseInfo) {
         String result = responseInfo.result;
         //在返回结果中获取用户的相关信息保存到共享参数中
         //处理完成后,根据判断,跳转至相应页面
        }
       });
      }
     });
    } catch (JSONException e) {
     e.printStackTrace();
    }
   }
  });
   
  break;
 }
}
 
}


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP