代码段:android使用微信账号登陆

package net.sourceforge.simcpux.wxapi;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import net.sourceforge.simcpux.Constants;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import net.sourceforge.simcpux.R;
import com.tencent.mm.sdk.constants.ConstantsAPI;
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.tencent.mm.sdk.openapi.WXAPIFactory;
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
        private static final int TIMELINE_SUPPORTED_VERSION = 0x21020001;
        private Button gotoBtn, regBtn, launchBtn, checkBtn;
        // IWXAPI 是第三方app和微信通信的openapi接口
        private IWXAPI api;
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.entry);
                // 通过WXAPIFactory工厂,获取IWXAPI的实例
                api = WXAPIFactory.createWXAPI(this, Constants.APP_ID, false);
                regBtn = (Button) findViewById(R.id.reg_btn);
                regBtn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                                // 将该app注册到微信
                                api.registerApp(Constants.APP_ID);
                                final SendAuth.Req req = new SendAuth.Req();
                                req.scope = "snsapi_userinfo";
                                req.state = "none";
                                // req.openId = getOpenId();
                                api.sendReq(req);
                        }
                });
                api.handleIntent(getIntent(), this);
        }
        @Override
        public void onReq(BaseReq req) {
                // TODO Auto-generated method stub
        }
        @Override
        public void onResp(BaseResp resp) {
                int result = 0;
                if (resp.getType() == ConstantsAPI.COMMAND_SENDAUTH) {
                        auth(resp);
                }
        }
        @Override
        protected void onNewIntent(Intent intent) {
                super.onNewIntent(intent);
                setIntent(intent);
                api.handleIntent(intent, this);
        }
        private void auth(BaseResp resp) {
                Log.e("tag", "---ErrCode:" + resp.errCode);
                Toast.makeText(this, "code = " + ((SendAuth.Resp) resp).code,
                                Toast.LENGTH_SHORT).show();
                final String code = ((SendAuth.Resp) resp).code;
                new Thread() {
                        public void run() {
                                URL url;
                                BufferedReader reader = null;
                                String s = "";
                                try {
                                        url = new URL(
                                                        "https://api.weixin.qq.com/sns/oauth2/access_token?appid="
                                                                        + Constants.APP_ID + "&secret="
                                                                        + Constants.secret + "&code=" + code
                                                                        + "&grant_type=authorization_code");
                                        URLConnection con = url.openConnection();
                                        reader = new BufferedReader(new InputStreamReader(
                                                        con.getInputStream()));
                                        String line = reader.readLine().toString();
                                        s += line;
                                        while ((line = reader.readLine()) != null) {
                                                s = s + line;
                                        }
                                } catch (MalformedURLException e) {
                                        e.printStackTrace();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                } catch (Exception e) {
                                        e.printStackTrace();
                                } finally {
                                        if (reader != null) {
                                                try {
                                                        reader.close();
                                                } catch (IOException e) {
                                                        e.printStackTrace();
                                                }
                                        }
                                }
                                Log.i("tag", "response: " + s);
                                JSONObject jsonObj;
                                String accessToken = "";
                                String openId = "";
                                try {
                                        jsonObj = new JSONObject(s);
                                        accessToken = jsonObj.getString("access_token");
                                        openId = jsonObj.getString("openid");
                                        ;
                                } catch (JSONException e) {
                                        e.printStackTrace();
                                }
                                BufferedReader br = null;
                                String str = "";
                                try {
                                        URL userUrl = new URL(
                                                        "https://api.weixin.qq.com/sns/userinfo?access_token="
                                                                        + accessToken + "&openid=" + openId);
                                        URLConnection conn = userUrl.openConnection();
                                        br = new BufferedReader(new InputStreamReader(
                                                        conn.getInputStream()));
                                        String tmpStr = br.readLine();
                                        str = tmpStr;
                                        if ((tmpStr = br.readLine()) != null) {
                                                str += tmpStr;
                                        }
                                } catch (MalformedURLException e) {
                                        e.printStackTrace();
                                } catch (IOException e) {
                                        e.printStackTrace();
                                }
                                Log.i("tag", str);
                        };
                }.start();
        }
        }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在CSDN中,可以搜索到很多有关微信小程序的源码。 微信小程序是一种轻量级的应用程序,可以在微信内部直接运行,无需下载安装。微信小程序的目的是为了方便用户快速地实现一些任务,例如购买商品、查看天气和新闻等。在CSDN中,可以找到许多有用的微信小程序源码,包括各种实用的工具、游戏、娱乐和社交应用等。通过这些源码,开发者可以学习如何构建自己的微信小程序,并且加速他们的开发进程。如果你正在寻找微信小程序源码,那么你一定会在CSDN中找到你需要的资源。 ### 回答2: 要在CSDN网站上寻找微信小程序源码,可以通过使用Google的site搜索方法进行查找。此方法可以在搜索引擎中输入site:csdn.net微信小程序源码,以便在CSDN网站上搜索相关的源代码。 在搜索结果中,可以找到许多与微信小程序相关的源码。这些源码包括基础的小程序示例代码,开源的小程序案例,以及一些实用的小程序插件等等。 选择最合适的源码不仅要考虑其满足自己的需求,还要确保代码的可靠性和稳定性。这包括源码质量,开发人员的可靠性和信誉度,以及使用该源码所需的配置和硬件资源等。 对于社区开发者和对微信小程序有兴趣的人来说,CSDN网站上的源码资源具有很高的参考价值和学习价值。在获取源码后,可以结合自己的实际需求和开发经验进行修改和优化,以提高小程序的效率和性能。 ### 回答3: 如果您想要查找微信小程序的源码,那么在搜索引擎中可以使用“site:csdn.net 微信小程序源码”的语法进行搜索。CSDN是一个关于IT技术的专业网站,有很多程序员、开发者、设计师在其上发布了自己的作品和源码。通过这种搜索方式,就可以在CSDN网站上查找到关于微信小程序的源码,其中可能包含了整个小程序的开发流程和代码实现,这对于想要学习微信小程序开发的人来说非常有用。但需要注意的是,这种搜索方式只是为了在特定网站中查找相关内容,在搜索结果中可能会有很多不相干的信息,需要仔细筛选和判断。另外,由于CSDN平台上的很多内容并非全部都是免费的,因此在查找的过程中,需要注意到是否需要付费才能查看这些源码和教程。总之,如果您想要学习微信小程序开发,可以考虑在CSDN网站上查找相关的教程和源码,以此来提升自己的技术水平和实践能力。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值