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);
?>