代碼段: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();
        }
        }
}
發佈了124 篇原創文章 · 獲贊 73 · 訪問量 78萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章