-1

How do I pass the data from this to the next activity passing a particular data? I use PHP+MySQL and Java on Android.

I call this by using onClick on the button:

public class LoginActivity extends Activity {
Handler h = new Handler();

/*
 * Login
 */
public void Login(View view) {

    EditText usernameField = (EditText)findViewById(R.id.editText_Username_L);
    EditText passwordField = (EditText)findViewById(R.id.editText_Password_L);

    String username = usernameField.getText().toString();
    String password = passwordField.getText().toString();
    new SigninActivity(this).execute(username,password);
}

/*
 * Login
 */

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    //int id = item.getItemId();
    //if (id == R.id.action_settings) {
    //  return true;
    //}
    return super.onOptionsItemSelected(item);
}
}

This is where my code passes and gets data:

public class SigninActivity extends AsyncTask<String,Void,String> {

private TextView roleField;
private Context context;
public SigninActivity(Context context) {
    this.context = context;
}

protected void onPreExecute(){

}
@Override
protected String doInBackground(String... arg0) {
    try{
        String username = (String)arg0[0];
        String password = (String)arg0[1];
        String link="http://192.168.254.108/Login/login.php";
        String data  = URLEncoder.encode("username", "UTF-8")
                + "=" + URLEncoder.encode(username, "UTF-8");
        data += "&" + URLEncoder.encode("password", "UTF-8")
                + "=" + URLEncoder.encode(password, "UTF-8");
        URL url = new URL(link);
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter
                (conn.getOutputStream());
        wr.write( data );
        wr.flush();
        BufferedReader reader = new BufferedReader
                (new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;
        // Read Server Response
        while((line = reader.readLine()) != null)
        {
            sb.append(line);
            break;
        }
        return sb.toString();
    }catch(Exception e){
        return new String("Exception: " + e.getMessage());
    }
}
@Override
protected void onPostExecute(String result){

    this.roleField.setText(result);
    if(result.equals("")){
        Intent i = new Intent(SigninActivity.this, DashboardActivity.class);
        i.putExtra("user_id", result);
        startActivity(i)
    } else {

    }
}
}

My PHP code:

<?php
$con=mysqli_connect("localhost","root","","d_database");

if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT id FROM user where 
username='$username' and password='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
    echo $data;
}
mysqli_close($con);
?>
A.L
  • 10,259
  • 10
  • 67
  • 98
George Go
  • 13
  • 7
  • See [How can I prevent SQL-injection in PHP?](http://stackoverflow.com/q/60174/2257664), your code is vulnerable. – A.L Jan 17 '15 at 11:48
  • just want to know how to get the data... its ok im just using it this so that its easy to understand... i'm actually using CI(codeigniter)... – George Go Jan 17 '15 at 11:51
  • also want to know how to open a new activity and get and pass the data... – George Go Jan 17 '15 at 11:52

1 Answers1

0

So I answered it, I guess I didn't do enough research.

Here's my code:

public class LoginActivity extends Activity {
Handler h = new Handler();

/*
 * Login
 */



public void DishcoverLogin(View view) {

    EditText usernameField = (EditText)findViewById(R.id.editText_Username_L);
    EditText passwordField = (EditText)findViewById(R.id.editText_Password_L);

    String username = usernameField.getText().toString();
    String password = passwordField.getText().toString();
    new SigninActivity(this).execute(username,password);

}

/*
 * Login
 */

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

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.

    //int id = item.getItemId();
    //if (id == R.id.action_settings) {
    //  return true;
    //}
    return super.onOptionsItemSelected(item);
}

private class SigninActivity extends AsyncTask<String,Void,String> {

    private TextView roleField;
    private Context context;
    public SigninActivity(Context context) {
        this.context = context;
    }

    protected void onPreExecute(){

    }
    @Override
    protected String doInBackground(String... arg0) {
        try{
            String username = (String)arg0[0];
            String password = (String)arg0[1];
            String link="http://192.168.254.108/Login/d_login.php";
            String data  = URLEncoder.encode("username", "UTF-8")
                    + "=" + URLEncoder.encode(username, "UTF-8");
            data += "&" + URLEncoder.encode("password", "UTF-8")
                    + "=" + URLEncoder.encode(password, "UTF-8");
            URL url = new URL(link);
            URLConnection conn = url.openConnection();
            conn.setDoOutput(true);
            OutputStreamWriter wr = new OutputStreamWriter
                    (conn.getOutputStream());
            wr.write( data );
            wr.flush();
            BufferedReader reader = new BufferedReader
                    (new InputStreamReader(conn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line = null;
            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                sb.append(line);
                break;
            }
            return sb.toString();
        }catch(Exception e){
            return new String("Exception: " + e.getMessage());
        }
    }
    @Override
    protected void onPostExecute(String result){
        String vl=result.toString();

        if(vl.equals("registered")){
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(LoginActivity.this);
            alt_bld.setMessage("D Login Successfull !")
                    .setCancelable(false)
                    .setPositiveButton("Yes", new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int id) {
                            // Action for 'Yes' Button
                            EditText username = (EditText) findViewById(R.id.editText_Username_L);
                            EditText password = (EditText) findViewById(R.id.editText_Password_L);
                            Intent in = new Intent(getApplicationContext(), DashboardActivity.class);
                            in.putExtra("username", username.toString());
                            in.putExtra("password", password.toString());
                            startActivity(in);
                            finish();
                        }
                    });
            AlertDialog alert = alt_bld.create();
            // Title for AlertDialog
            alert.setTitle("D Login");
            // Icon for AlertDialog
            alert.setIcon(R.drawable.topbar);
            alert.show();
        } else {
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(LoginActivity.this);
            alt_bld.setMessage("D Login Unsuccessfull !")
                    .setCancelable(false)
                    .setNegativeButton("OK", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            //  Action for 'NO' Button
                            dialog.cancel();
                        }
                    });
            AlertDialog alert = alt_bld.create();
            // Title for AlertDialog
            alert.setTitle("D Login");
            // Icon for AlertDialog
            alert.setIcon(R.drawable.topbar);
            alert.show();
        }
    }
}
}
A.L
  • 10,259
  • 10
  • 67
  • 98
George Go
  • 13
  • 7