代码段:android使用微信账号登陆
2015-12-28 14:13
621 查看
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(); } } }
相关文章推荐
- 社交巨头三国杀:微信、WhatsApp、Line到底有啥区别?
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件