public class LoginActivity extends AppCompatActivity {
ViewFlipper Vf;
Button loginButton, registerButton;
EditText idText, passwordText;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
loginButton = (Button)findViewById(R.id.loginButton);
registerButton = (Button)findViewById(R.id.registerButton);
idText = (EditText)findViewById(R.id.idText);
passwordText = (EditText)findViewById(R.id.passwordText);
loginButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(LoginActivity.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login() {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("http://root/login.php");
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username", idText.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("password", passwordText.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if (response.equalsIgnoreCase("User Found")) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(LoginActivity.this, "Login Success", Toast.LENGTH_SHORT).show();
}
});
// 로그인 성공시 MainActivity 전환
startActivity((new Intent(LoginActivity.this, MainActivity.class)));
finish();
} else {
Toast.makeText(LoginActivity.this, "Login Fail", Toast.LENGTH_SHORT).show();
}
}
catch(Exception e)
{
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void Register(View view)
{
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
}
}
login.php
<?php
$connect = @mysql_connect("localhost","root","apmsetup") or die("error");
$dbname = "mydb";
$dbconn = mysql_select_db($dbname,$connect);
$username = $_POST['username'];
$query_search = "select * from custom_info where u_id = '".$username."' AND u_pw = '".$password. "'";
$password = $_POST['password'];
$query_exec = mysql_query($query_search) or die(mysql_error());
echo "User Found";
$rows = mysql_num_rows($query_exec);
if($rows == 0) {
echo "No Such User Found";
}
else {
}
?>
DB에는 잘 들어가는데...
여기서 회원가입후 로그인하면 액티비티가 RegisterAcitivty.java 로 넘어가는데
액티비티 전환 소스 제대로 했는데 왜 회원가입창으로 다시 넘어가나요ㅠㅠ?
회원가입할때 만약 DB에 있는 id 와 회원가입하려는 id 값이 같으면 이미 있다고 창 뜨는 거랑
로그인시 입력한 아이디와 패스워드 DB에 없으면 로그인 실패라는 창 띄우려는데 어떻게 써야하죠..?
더 좋은 회원가입 소스 있으면 url 남겨주시면 감사하겠습니다..