1

i use this code to store the name and password in login activity!

 public void main_login(View v)
{
    Name = name.getText().toString();
    Password = password.getText().toString();
    BackGround b = new BackGround();
    b.execute(Name, Password);


    context = getApplicationContext();
    sp = getSharedPreferences("identifier", context.MODE_PRIVATE);
    SharedPreferences.Editor Ed= sp.edit();
    Ed.putString("Name",name.getText().toString()).commit();
    Ed.putString("Password",  password.getText().toString()).commit();
   // startActivity(new Intent(Login.this,Reg_Token_Id.class));
    startActivity(new Intent(this, Reg_Token_Id.class));

}

i got the name ,password,token ushing sharedprefences,and sending them to php file to insert the id in the table.i am able to make a toast and able to see the name ,token ,password, but not able to send them to php file

can anyone help me to fix this issue?

selva surya
  • 103
  • 9

3 Answers3

1

You need to use this instead of PreferenceManager.getDefaultSharedpreferences()

SharedPreference sp = context.getSharedPreferences("Identifier",Context.MODE_PRIVATE)

and call shared preferences with this "Identifier" in another activity

La Win Ko
  • 254
  • 2
  • 10
  • where i should put this code ,i used this code in login activity,but showing error !can you please edit my code? – selva surya Feb 24 '18 at 04:49
  • I think u used with context.I am just showing example with context.In Acitivity,you can use this.getSharedPreferences() – La Win Ko Feb 24 '18 at 04:52
  • can you check my second activity weather i am using correct code to recieve them? – selva surya Feb 24 '18 at 04:55
  • First use SharedPreference sp = this.getSharedPreferences("Identifier",Context.MODE_PRIVATE) and get data with String name = sp.getString("Name","") , you will be receive data with this – La Win Ko Feb 24 '18 at 04:58
1

This is the example to save data in sharedpreferences and get the data back from sharedpreferences, hope it will help you out. Try using your own preference name instead of using PreferenceManager.getDefaultSharedPreferences(getBaseContext())

 public class BChooserPreferences {
        private final static String FILE = "b_chooser_prefs";
        private final static String FOLDER_NAME = "folder_name";
        private SharedPreferences preferences;

        public BChooserPreferences(Context context) {
            preferences = context.getSharedPreferences(FILE, Context.MODE_PRIVATE);
        }

        /**
         * Set the folder name to be used for all files or temporary files
         * @param folderName
         */
        public void setFolderName(String folderName){
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString(FOLDER_NAME, folderName);
            editor.commit();
        }

        public String getFolderName(){
            return preferences.getString(FOLDER_NAME, "bichooser");
        }

    }
La Win Ko
  • 254
  • 2
  • 10
Milan Hirpara
  • 534
  • 4
  • 18
1

Change your login.java as below.

SharedPreferences sp = getSharedPreferences("identifier", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed= sp.edit();
Ed.putString("Name", Name).commit();
Ed.putString("Password", Password).commit();

You can use any word you like for identifier. using this identifier you can retrieve SharedPreferences in your second activity as follows.

SharedPreferences  mPrefs = getSharedPreferences("identifier", Context.MODE_PRIVATE);
String Name = mPrefs.getString("Name", null);
String Password = mPrefs.getString("Password", null);

Using above you will be able to deal with SharedPreferences correctly.

I have changed your backend php code as bellow. Compare it with your code.

<?php
 require "init.php";

    $token = $_POST["Token"];
    $Name = $_POST["Name"];
    $Password = $_POST["Password"];
    $user_id;

    $Sql = "SELECT * FROM `user_info` 
        WHERE `name`='".$Name."' AND 
        `password`='".$Password."';";

    $result = mysqli_query($con, $Sql);
    $retrive = array();
    while($row = mysqli_fetch_array($result))
    {
     $user_id =  $row['id']; 
    }

    $query ="INSERT INTO user(Token,Employeeid) Values ('".$token."', 
    '".$user_id."')";

    if (mysqli_query($con, $query)) {
    echo "New record created successfully";
    } else {
    echo "Error: " . $query . "<br>" . mysqli_error($con);
    }
    mysqli_close($con);
?>

If you have any doubt feel free to comment.

Sudarshana Dayananda
  • 5,165
  • 2
  • 23
  • 45
  • i used this code but the app getting crushed,i have edited my code and i had put them above with logcat please check it! – selva surya Feb 24 '18 at 06:23
  • In your registerToken(String token) method you are putting again name and password to SharedPreferences again. you need to get name and password from SharedPreferences in that method. Change it according to my answer and run your code again. – Sudarshana Dayananda Feb 24 '18 at 06:36
  • wait sir i will try your code in new sample project to check weather it is working correct are not! – selva surya Feb 24 '18 at 06:50
  • ok dude. check and update me. i will try to give an answer for your question. – Sudarshana Dayananda Feb 24 '18 at 06:53
  • same error the app is getting closed, i have edited the code of new project mainactivity and main2 activity please check weather i am doing correct – selva surya Feb 24 '18 at 07:01
  • give me few minuetes. – Sudarshana Dayananda Feb 24 '18 at 07:12
  • add context = getApplicationContext(); inside onCreate method of Main2Activity. – Sudarshana Dayananda Feb 24 '18 at 07:40
  • sir the code is working i can able use them in main activity,but when i try to put the code in main project,same problem app closed!,i think there is a problem in sending the name and password to php file.can you check weather i am sending them correctly – selva surya Feb 24 '18 at 07:58
  • if your logcat is latest one, issue is not with sending data to backend. update your question with the full code of Main2Activity because issue is with starting activity. – Sudarshana Dayananda Feb 24 '18 at 08:08
  • sir i have found a way to insert the id,but not able to send the name,password ,token to php can you check them ,i have edited the code – selva surya Feb 24 '18 at 09:52
  • Try code debugging and find the issue and search google and you will find a solution by yourself and you will be a good developer. – Sudarshana Dayananda Feb 24 '18 at 10:11