2016-07-18 7 views
-4

Ich weiß nicht, warum nach dem Login in Hauptaktivität Benutzername und Passwort nicht angezeigt ..... Ich sehe nur logout Button ... warum?Name und Passwort nicht in der Haupttätigkeit nach dem Login

Ich hoffe, dass Sie mir helfen können!

LOGIN ACTIVITY:

public class LoginActivity extends Activity { 
    private static final String TAG = RegisterActivity.class.getSimpleName(); 
    private Button btnLogin; 
    private Button btnLinkToRegister; 
    private EditText inputNumeroTelefonico; 
    private EditText inputPassword; 
    private ProgressDialog pDialog; 
    private SessionManager session; 
    private SQLiteHandler db; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     inputNumeroTelefonico = (EditText) findViewById(R.id.numero_telefonico); 
     inputPassword = (EditText) findViewById(R.id.password); 
     btnLogin = (Button) findViewById(R.id.btnLogin); 
     btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen); 

     // Progress dialog 
     pDialog = new ProgressDialog(this); 
     pDialog.setCancelable(false); 

     // SQLite database handler 
     db = new SQLiteHandler(getApplicationContext()); 

     // Session manager 
     session = new SessionManager(getApplicationContext()); 

     // Check if user is already logged in or not 
     if (session.isLoggedIn()) { 
      // User is already logged in. Take him to main activity 
      Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
      startActivity(intent); 
      finish(); 
     } 

     // Login button Click Event 
     btnLogin.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       String numero_telefonico = inputNumeroTelefonico.getText().toString().trim(); 
       String password = inputPassword.getText().toString().trim(); 

       // Check for empty data in the form 
       if (!numero_telefonico.isEmpty() && !password.isEmpty()) { 
        // login user 
        checkLogin(numero_telefonico, password); 
       } else { 
        // Prompt user to enter credentials 
        Toast.makeText(getApplicationContext(), 
          "Please enter the credentials!", Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 

     }); 

     // Link to Register Screen 
     btnLinkToRegister.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(), 
         RegisterActivity.class); 
       startActivity(i); 
       finish(); 
      } 
     }); 

    } 

    /** 
    * function to verify login details in mysql db 
    * */ 
    private void checkLogin(final String numero_telefonico, final String password) { 
     // Tag used to cancel the request 
     String tag_string_req = "req_login"; 

     pDialog.setMessage("Logging in ..."); 
     showDialog(); 

     StringRequest strReq = new StringRequest(Method.POST, 
       AppConfig.URL_LOGIN, new Response.Listener<String>() { 

      @Override 
      public void onResponse(String response) { 
       Log.d(TAG, "Login Response: " + response.toString()); 
       hideDialog(); 

       try { 
        JSONObject jObj = new JSONObject(response); 
        boolean error = jObj.getBoolean("error"); 

        // Check for error node in json 
        if (!error) { 
         // user successfully logged in 
         // Create login session 
         session.setLogin(true); 

         // Now store the user in SQLite 
         String uid = jObj.getString("uid"); 

         JSONObject user = jObj.getJSONObject("user"); 
         String name = user.getString("name"); 
         String cognome = user.getString("cognome"); 

         String numero_telefonico = user.getString("numero_telefonico"); 
         String created_at = user 
           .getString("created_at"); 

         // Inserting row in users table 
         db.addUser(name,cognome, numero_telefonico, uid, created_at); 

         // Launch main activity 
         Intent intent = new Intent(LoginActivity.this, 
           MainActivity.class); 
         startActivity(intent); 
         finish(); 
        } else { 
         // Error in login. Get the error message 
         String errorMsg = jObj.getString("error_msg"); 
         Toast.makeText(getApplicationContext(), 
           errorMsg, Toast.LENGTH_LONG).show(); 
        } 
       } catch (JSONException e) { 
        // JSON error 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 

      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Login Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_LONG).show(); 
       hideDialog(); 
      } 
     }) { 

      @Override 
      protected Map<String, String> getParams() { 
       // Posting parameters to login url 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("numero_telefonico", numero_telefonico); 
       params.put("password", password); 

       return params; 
      } 

     }; 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 
    } 

    private void showDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hideDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

HAUPTTÄTIGKEIT:

public class MainActivity extends Activity { 

    private TextView txtName; 
    private TextView txtNumeroTelefonico; 
    private Button btnLogout; 

    private SQLiteHandler db; 
    private SessionManager session; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     txtName = (TextView) findViewById(R.id.name); 
     txtNumeroTelefonico = (TextView) findViewById(R.id.numero_telefonico); 
     btnLogout = (Button) findViewById(R.id.btnLogout); 

     // SqLite database handler 
     db = new SQLiteHandler(getApplicationContext()); 

     // session manager 
     session = new SessionManager(getApplicationContext()); 

     if (!session.isLoggedIn()) { 
      logoutUser(); 
     } 

     // Fetching user details from SQLite 
     HashMap<String, String> user = db.getUserDetails(); 

     String name = user.get("name"); 
     String numero_telefonico = user.get("numero_telefonico"); 

     // Displaying the user details on the screen 
     System.out.println(name+""+numero_telefonico); 

     txtName.setText(name); 
     txtNumeroTelefonico.setText(numero_telefonico); 

     // Logout button click event 
     btnLogout.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       logoutUser(); 
      } 
     }); 
    } 

    /** 
    * Logging out the user. Will set isLoggedIn flag to false in shared 
    * preferences Clears the user data from sqlite users table 
    * */ 
    private void logoutUser() { 
     session.setLogin(false); 

     db.deleteUsers(); 

     // Launching the login activity 
     Intent intent = new Intent(MainActivity.this, LoginActivity.class); 
     startActivity(intent); 
     finish(); 
    } 
} 

LOGIN ACTIVITY.XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:padding="10dp" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_gravity="center_vertical" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:paddingTop="56dp" 
     android:paddingLeft="24dp" 
     android:paddingRight="24dp" > 
     <ImageView 
      android:background="@drawable/signor_pomidor" 
      android:layout_gravity="center_horizontal" 
      android:layout_width="150dp" 
      android:layout_height="150dp" /> 
     <EditText 
      android:id="@+id/numero_telefonico" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:background="@color/btn_login_bg" 
      android:hint="@string/hint_numero_telefonico" 
      android:inputType="textEmailAddress" 
      android:padding="10dp" 
      android:singleLine="true" 
      android:textColor="@color/input_login" 
      android:textColorHint="@color/input_login_hint" /> 

     <EditText 
      android:id="@+id/password" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:background="@color/btn_login_bg" 
      android:hint="@string/hint_password" 
      android:inputType="textPassword" 
      android:padding="10dp" 
      android:singleLine="true" 
      android:textColor="@color/input_login" 
      android:textColorHint="@color/input_login_hint" /> 

     <!-- Login Button --> 

     <Button 
      android:id="@+id/btnLogin" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="20dip" 
      android:background="@color/btn_login_bg" 
      android:text="@string/btn_login" 
      android:textColor="@color/btn_logut_bg" /> 

     <!-- Link to Login Screen --> 

     <Button 
      android:id="@+id/btnLinkToRegisterScreen" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="40dp" 
      android:background="@null" 
      android:text="@string/btn_link_to_register" 
      android:textAllCaps="false" 
      android:textColor="@color/btn_logut_bg" 
      android:textSize="15dp" /> 

    </LinearLayout> 

</ScrollView> 

MAIN ACTIVITY.XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="${relativePackage}.${activityClass}" > 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:gravity="center" 
     android:orientation="vertical" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/welcome" 
      android:textSize="20dp" /> 

     <TextView 
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:textColor="@color/lbl_name" 
      android:textSize="24dp" /> 

     <TextView 
      android:id="@+id/numero_telefonico" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="13dp" /> 

     <Button 
      android:id="@+id/btnLogout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="40dip" 
      android:background="@color/btn_logut_bg" 
      android:text="@string/btn_logout" 
      android:textAllCaps="false" 
      android:textColor="@color/white" 
      android:textSize="15dp" /> 
    </LinearLayout> 

</RelativeLayout> 

GET USER DETAIL: 

if (isset ($ _ POST [ 'numero_telefonico']) & & isset ($ _ POST [ 'password'])) {

// receiving the post params 
    $numero_telefonico = $_POST['numero_telefonico']; 
    $password = $_POST['password']; 

    // get the user by email and password 
     $user = $db->getUserByEmailAndPassword($numero_telefonico, $password); 

     if ($user != false) { 
      // use is found 
      $response["error"] = FALSE; 
      $response["uid"] = $user["unique_id"]; 
      $response["user"]["name"] = $user["name"]; 
      $response["user"]["cognome"] = $user["cognome"]; 
      $response["user"]["numero_telefonico"] = $user["numero_telefonico"]; 
      $response["user"]["created_at"] = $user["created_at"]; 
      $response["user"]["updated_at"] = $user["updated_at"]; 
      echo json_encode($response); 
     } 




public function getUserByEmailAndPassword($numero_telefonico, $password) { 

     $stmt = $this->conn->prepare("SELECT * FROM users WHERE numero_telefonico = ?"); 

     $stmt->bind_param("s", $numero_telefonico); 

     if ($stmt->execute()) { 
      $user = $stmt->get_result()->fetch_assoc(); 
      $stmt->close(); 

      // verifying user password 
      $salt = $user['salt']; 
      $encrypted_password = $user['encrypted_password']; 
      //$hash = $this->checkhashSSHA($salt, $password); 
         $hash = $password; 

      // check for password equality 
      if ($encrypted_password == $hash) { 
       // user authentication details are correct 
       return $user; 
      } 
     } else { 
      return NULL; 
     } 
    } 

enter image description here

+2

Bitte nur den ** wichtigen ** Code und nicht alle davon anhängen. Beschreiben Sie auch Ihre Fehler, was genau passiert? Versuchen Sie, im Debug-Modus zu laufen und setzen Sie Haltepunkte, um zu sehen, wo die Daten verloren gehen. – Vucko

+1

Können Sie Ihre getUserDetails() -Methode posten – Punithapriya

+0

Wie erhalten Sie die spezifischen Benutzerdetails? – Sanoop

Antwort

1

Ausgabe in Ihrem MAIN ACTIVITY.XML Folgen Sie einfach die folgenden Codes Ich habe das Gewicht & einige der Farben geändert, aber Sie können es gemäß Ihrer Anforderung ändern. Im Hauptrelativ-Layout habe ich den Hintergrund weiß gewählt.

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_marginLeft="20dp" 
    android:layout_marginRight="20dp" 
    android:gravity="center" 
    android:orientation="vertical" 
    android:weightSum="4"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/welcome" 
     android:textSize="20dp" 
     android:textColor="@color/black" 
     android:layout_weight="1"/> 

    <TextView 
     android:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:textColor="@color/black" 
     android:textSize="24dp" 
     android:text="@string/welcome" /> 

    <TextView 
     android:id="@+id/numero_telefonico" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="13dp" 
     android:layout_weight="1" 
     android:textColor="@color/black" 
     android:text="@string/welcome"/> 

    <Button 
     android:id="@+id/btnLogout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="40dip" 
     android:background="@color/btn_logut_bg" 
     android:text="@string/btn_logout" 
     android:textAllCaps="false" 
     android:textColor="@color/black" 
     android:textSize="15dp" 
     android:layout_weight="1" /> 
</LinearLayout> 

Hope this u helfen.

+0

Sorry, ich verstehe nicht ... wo ist meine iusse? Danke – pappa

+0

Ich füge Ihren Code, aber es funktioniert nicht selbe .... – pappa

+0

Während das Abrufen der Daten von hashmap ist dort irgendeinen Namen enthalten? – Andolasoft

Verwandte Themen