-4

Here i initialize get the value of user input after btnlog onclick

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

    usernameField = (EditText) findViewById(R.id.etUname);
    passwordField = (EditText) findViewById(R.id.etUpass);

    btnlog = (Button) findViewById(R.id.loginbtn);
    btnview = (Button) findViewById(R.id.viewbtn);
    reglink = (TextView) findViewById(R.id.reglink);

    btnlog.setOnClickListener (new View.OnClickListener(){
        @Override
        public void onClick(View v){

            String u = usernameField.getText().toString();
            String p = passwordField.getText().toString();

            new loginPost().execute();
        }
    });

Now i want to received the user input from public class and encode it, and the variable u and p is basically doesnt getting any value from public class

@Override
        protected String doInBackground(String... params) {
            try{

            String str = "username=" + u + "&password=" + p;
            String data = URLEncoder.encode("str", "UTF-8") + "=" +
                    URLEncoder.encode(str, "UTF-8");
            strUrl ="http://10.0.2.2/android/login.php?"+data+"";
            Log.d("STR", data);


            URL url = new URL (strUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.connect();
ZhanWei Yap
  • 3
  • 1
  • 2
  • 4

3 Answers3

1

Define u and p outside onCreate() and declare it public. Now you can access these variable from other class by

    this.p=MainActivity.p;
    this.u=MainActivity.u;
joshiparas
  • 141
  • 1
  • 1
  • 8
  • What if I have class1 and class2 at the same activity? Your answer will return to this.x = Class.x = this.x for example? – Bay Sep 22 '19 at 17:43
0

my java is not very fluent but it looks like you are trying to pass variables to an asynctask, if that is the case then take a look here, How to pass variables in and out of AsyncTasks? It shows how to pass variables on a .execute() call

Community
  • 1
  • 1
Tsepo Nkalai
  • 1,492
  • 11
  • 18
0

You can pass it in .execute() method because that is what is design for.

btnlog.setOnClickListener (new View.OnClickListener(){
        @Override
        public void onClick(View v){
            String u = usernameField.getText().toString();
            String p = passwordField.getText().toString();
            new loginPost().execute(u,p);
        }
});

Then in your doInBackground()

@Override
protected String doInBackground(String... params) {
    try{

    String str = "username=" + params[0] + "&password=" + params[1];
    String data = URLEncoder.encode("str", "UTF-8") + "=" +
            URLEncoder.encode(str, "UTF-8");
    strUrl ="http://10.0.2.2/android/login.php?"+data+"";
    Log.d("STR", data);


    URL url = new URL (strUrl);
    HttpURLConnection con = (HttpURLConnection) url.openConnection();
    con.setRequestMethod("POST");
    con.connect();
Enzokie
  • 7,365
  • 6
  • 33
  • 39