java 我怎样才能得到谁是我的应用程序登录用户的ID?

x33g5p2x  于 2023-01-15  发布在  Java
关注(0)|答案(2)|浏览(140)

我怎样才能得到谁是我的应用程序登录用户的ID?
我有两个活动。第一个是登录活动,第二个是注册活动。我也使用JSON。如果用户注册成功,那么它会创建一个ID。我需要在我的登录页面的ID。
Register.java

protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
         int success;
         String firstname = first.getText().toString();
         String lastname = last.getText().toString();
         String emailaddress = emailet.getText().toString();
         String username = user.getText().toString();
         String password= passet.getText().toString();
         try {
              List params = new ArrayList();
              params.add(new BasicNameValuePair("first_name", firstname));
              params.add(new BasicNameValuePair("last_name", lastname));
              params.add(new BasicNameValuePair("user_email", emailaddress));
              params.add(new BasicNameValuePair("user_name", username));
              params.add(new BasicNameValuePair("user_pass", password));
              
              Log.d("request!", "starting");
              JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST", params);
              Log.d("Login attempt", json.toString());
              success = json.getInt(TAG_SUCCESS);
              if (success == 1) {
                  Log.d("User Created!", json.toString());   
       ;
              
                  
                  
                  Toast.makeText(getApplicationContext(), json.toString(), Toast.LENGTH_SHORT).show();
                  
                  finish();
                  return json.getString(TAG_MESSAGE);
              }else{
                  Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                  return json.getString(TAG_MESSAGE);
                  
              }
              
              
        } catch (Exception e) {
            // TODO: handle exception
             e.printStackTrace();
        }
         
        return null;
    }

Login.java

protected String doInBackground(String...args) {
        // TODO Auto-generated method stub
        int success;
        String username = user.getText().toString();
        String password = pass.getText().toString();
        try {
            List params = new ArrayList();
            params.add(new BasicNameValuePair("user_email", username));
            params.add(new BasicNameValuePair("user_pass", password) );
            Log.d("request!", "starting");
            JSONObject json = jsonParse.makeHttpRequest(LOGIN_URL, "GET", params);
              Log.d("Login attempt", json.toString());

            
              success = json.getInt(TAG_SUCCESS);
              if (success == 1) {
                  Log.d("Login Successful!", json.toString());
               
                  return json.getString(TAG_MESSAGE);
              }else{
                  Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                  return json.getString(TAG_MESSAGE);      
              }
        } catch (Exception e) {
            // TODO: handle exception
        }
          
        return null;
    }
cld4siwp

cld4siwp1#

首先需要将该ID存储在数据库中的某个位置,然后在登录时需要将该ID与用户名和密码一起传递

a64a0gku

a64a0gku2#

将其放入会话管理器。

public static void saveSession_userid(SharedPreferences prefs,String value) {
    SessionManager.savePreference(prefs, USERID, value);
}

public static String get_session_userid(SharedPreferences prefs) {
    return prefs.getString(USERID, "");
}

这在PostExecute()上的登录异步任务中

if (login_result.equals("sucess")) {
id = jsonObject.getString("userId");
SessionManager.saveSession_userid(prefs, id);

并宣布:

SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(this);

相关问题