此代码用于登录,将连接到php并获取用户的详细信息,以便他可以继续进行下一个活动
的Android
public class MainActivity extends Activity {
EditText etUsername, etPassword;
TextView tvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = (EditText)findViewById(R.id.editText1);
etPassword = (EditText)findViewById(R.id.editText2);
tvResult = (TextView)findViewById(R.id.textView1);
final Button bt = (Button)findViewById(R.id.secret);
bt.setVisibility(View.INVISIBLE);
bt.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Home.class));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onclick(View v){
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();
String command = "Login";
// eto yung parameters na ipapasa nyo sa php file POST method po gamit dito
try {
String data;
data = URLEncoder.encode("getuser", "UTF-8")
+ "=" + URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("getpass", "UTF-8")
+ "=" + URLEncoder.encode(password, "UTF-8");
data += "&" + URLEncoder.encode("command", "UTF-8")
+ "=" + URLEncoder.encode(command, "UTF-8");
LoginToOmega ltg = new LoginToOmega();
ltg.execute(data);
try {
tvResult.setText(ltg.get());
if(tvResult.equals("Login Accepted!")){
startActivity(new Intent(MainActivity.this, Home.class));
View bt = (Button)findViewById(R.id.secret);
bt .setVisibility(View.VISIBLE);
}
System.out.println(ltg.get());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
class LoginToOmega extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
try{
//location ng php file
// String link="http://10.0.3.2/eMobile/login.php";
String link="http://192.168.1.2/omega_connect.php";
// String link = "http://192.168.56.101/eMobile/login.php";
URL url = new URL(link);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wrte = new OutputStreamWriter
(conn.getOutputStream());
wrte.write( params[0]);
wrte.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){
e.toString();
PHP
<?php
DEFINE('server', "119.92.76.18");
DEFINE('username', "comgrap");
DEFINE('password', "comgrap");
DEFINE('db', "omega");
/*DEFINE('server', "localhost");
DEFINE('username', "root");
DEFINE('password', "");
DEFINE('db', "omega");*/
$con = mysqli_connect(server, username, password, db);
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['command'])) {
$username = $_POST['getuser'];
$password = $_POST['getpass'];
$command = $_POST['command'];
switch ($command) {
case 'Login':
Login($con, $username, $password);
break;
}
}
function Login($con, $username, $password) {
$year_now = date("Y");
$last_year = $year_now - 1;
$next_year = $year_now + 1;
$school_year = $last_year . '-' . $year_now;
$school_year1 = $year_now . '-' . $next_year;
$subject = "PHILMAN";
$query = "SELECT COUNT(`student_no`) FROM `stcred_accounts` WHERE `student_no` = '$username' AND `accepted` = '1'";
$query_data = mysqli_query($con,$query);
$data_row = mysqli_fetch_array($query_data);
if($data_row[0]>0){
$sql = "SELECT COUNT(`student_no`) FROM `stcred_accounts` WHERE `student_no` = '$username' AND `password` = MD5('$password') ";
$sql_query = mysqli_query($con,$sql);
$query_row = mysqli_fetch_array($sql_query);
if($query_row[0]>0){
$query3 = "SELECT COUNT(`student_no`) FROM `dbo_class_list` WHERE `student_no` = '$username' AND `subject_code` = '$subject' AND `school_year` = '$school_year' OR `school_year` = '$school_year1'";
$row3 = mysqli_query($con, $query3);
$result3 = mysqli_fetch_array($row3);
if($result3[0]>0)
{
echo "Login Accepted!";
}
else
{
echo "You are not a student of PHILMAN.";
}
}
else
{
echo "Invalid StudentId or Password";
}
}
else
{
echo "Identification Error: Invalid StudentID";
}
}
?>
return e.toString();
}
}
}
}
我的问题是当我点击button1它只显示登录被接受但我真正想要的是它将移动到下一个活动我在哪里放置startactivty请帮助我这是我的项目如果我不能这样做也许我将在决赛中失败