团队冲刺第七天--校园百晓生(STDeverything)
摘要:获取了第三方登录返回数据的openid、昵称和40*40像素头像(以地址形式保存),并将其存入云服务器数据库,由于一天满课,实现功能还需完善。
代码:
QQToken qqToken = mTencent.getQQToken(); UserInfo info = new UserInfo(getApplicationContext(), qqToken); userId = mTencent.getOpenId();//获取唯一标识符 Log.v("userId",userId); // info.getUserInfo(new BaseUIListener(this,"get_simple_userinfo")); info.getUserInfo(new IUiListener() { @Override public void onComplete(Object o) { //用户信息获取到了 try { Toast.makeText(getApplicationContext(), ((JSONObject) o).getString("nickname")+((JSONObject) o).getString("gender"), Toast.LENGTH_SHORT).show(); Log.v("UserInfo",o.toString()); username=((JSONObject) o).getString("nickname");//获取昵称 Log.v("username",username); userimage=((JSONObject) o).getString("figureurl_qq_1");//获取头像 Log.v("userimage",userimage); Intent intent1 = new Intent(Login.this,MainActivity.class); startActivity(intent1); finish(); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); }
获取用户基本信息
private final static String requrl="http://#/STDEverything/login_servlet"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_register); FloatingActionButton register_btn = findViewById(R.id.register_btn); name_text=findViewById(R.id.name); pwd_text=findViewById(R.id.pwd); handler=new RegisterHandler(this); register_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=name_text.getText().toString(); String pwd=pwd_text.getText().toString(); final String reqdata="method=login&name="+name+"&pwd="+pwd+""; new Thread(new Runnable() { @Override public void run() { try { JSONObject jsonObject=new JSONObject(String.valueOf(HttpUtil.sendPost(requrl,reqdata))); Message msg=new Message(); msg.what=100; msg.obj=jsonObject; handler.sendMessage(msg); } catch (JSONException e) { e.printStackTrace(); } } }).start(); } }); } static class RegisterHandler extends Handler{ private final WeakReference<Register> mcontext; RegisterHandler(Register context){ mcontext=new WeakReference<>(context); } @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if(msg.what==100){ JSONObject jsonObject= (JSONObject) msg.obj; try { if(jsonObject.get("name").equals("yes")) Toast.makeText(mcontext.get(),"用户名已存在",Toast.LENGTH_SHORT).show(); else if(jsonObject.get("name").equals("no")){ new Thread(new Runnable() { @Override public void run() { String name=mcontext.get().name_text.getText().toString(); String pwd=mcontext.get().pwd_text.getText().toString(); final String reqdata="method=register&name="+name+"&pwd="+pwd+""; HttpUtil.sendPost(requrl,reqdata); sendEmptyMessage(888); } }).start(); } } catch (JSONException e) { e.printStackTrace(); }
存入云数据库(待完善)
明天加油!