标签:
1.防坑指南:
<activity android:name="net.sourceforge.simcpux.wxapi.WXEntryActivity" android:configChanges="orientation|keyboardHidden" android:exported="true" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@android:style/Theme.Translucent.NoTitleBar" > <intent-filter> <action android:name="android.intent.action.VIEW" > </action> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
private void handleIntent(Intent intent) { String result = ""; ConUrls.WriteLog("handleIntent"+result); SendAuth.Resp resp = new SendAuth.Resp(intent.getExtras()); if (resp.errCode == BaseResp.ErrCode.ERR_OK) { //用户同意 result ="发送成功"; //ConUrls.WriteLog("handleIntent"+result); //Contexts.CurWeiXinHelper; String weixinCode = resp.code; ConUrls.WriteLog("onResume "+weixinCode); if(Contexts.CurWeiXinHelper !=null) { ConUrls.WriteLog("CurWeiXinHelper "+weixinCode); try { Contexts.CurWeiXinHelper.GetLoginInfo(weixinCode); Contexts.CurWeiXinHelper=null; finish(); } catch(Exception e) { ConUrls.WriteLog(e.getMessage()); } } } }
5.如何根据code 拿到 openId?
private void WXGetAccessToken(){ HttpClient get_access_token_httpClient = new DefaultHttpClient(); HttpClient get_user_info_httpClient = new DefaultHttpClient(); String access_token=""; String openid =""; try { HttpPost postMethod = new HttpPost(get_access_token); HttpResponse response = get_access_token_httpClient.execute(postMethod); // 执行POST方法 if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream is = response.getEntity().getContent(); BufferedReader br = new BufferedReader( new InputStreamReader(is)); String str = ""; StringBuffer sb = new StringBuffer(); while ((str = br.readLine()) != null) { sb.append(str); } is.close(); String josn = sb.toString(); JSONObject json1 = new JSONObject(josn); access_token = (String) json1.get("access_token"); openid = (String) json1.get("openid"); WeiXinAuthLoginModel model=new WeiXinAuthLoginModel(); model.setOpenId(openid); LoginModel MyLoginModel= SysHelper.GetWeiXinLogin(model, curContent); Contexts.MyLoginModel = MyLoginModel; SysHelper.StartAm(curContent); if(mHandler!=null) { Message msg=new Message(); msg.obj=MyLoginModel; msg.arg1=mHandlerOrg1; msg.what=1; mHandler.sendMessage(msg); } } else { } } catch (UnsupportedEncodingException e) { e.printStackTrace(); if(mHandler!=null) { Message msg=new Message(); msg.obj="登录失败"; msg.arg1=mHandlerOrg1; msg.arg2=-1; msg.what=0; mHandler.sendMessage(msg); } } catch (ClientProtocolException e) { e.printStackTrace(); if(mHandler!=null) { Message msg=new Message(); msg.obj="登录失败"; msg.arg1=mHandlerOrg1; msg.arg2=-1; msg.what=0; mHandler.sendMessage(msg); } } catch (IOException e) { e.printStackTrace(); if(mHandler!=null) { Message msg=new Message(); msg.obj="登录失败"; msg.arg1=mHandlerOrg1; msg.arg2=-1; msg.what=0; mHandler.sendMessage(msg); } } catch (JSONException e) { e.printStackTrace(); if(mHandler!=null) { Message msg=new Message(); msg.obj="登录失败"; msg.arg1=mHandlerOrg1; msg.arg2=-1; msg.what=0; mHandler.sendMessage(msg); } } //不获取 info // String get_user_info_url=getUserInfo(access_token,openid); // WXGetUserInfo(get_user_info_url); }
标签:
原文地址:http://www.cnblogs.com/shikyoh/p/4505800.html