Skip to content

Instantly share code, notes, and snippets.

@luhaiwork
Last active February 6, 2021 14:46
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save luhaiwork/fd9c2013949d26be98c0 to your computer and use it in GitHub Desktop.
Save luhaiwork/fd9c2013949d26be98c0 to your computer and use it in GitHub Desktop.
Wechat Login
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();
}
}
@NayabSarfraz
Copy link

onReq,onResp never get invoked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment