public class LogInActivity extends Activity implements OnClickListener{
private TextView titleTextView;
private TextView joinTextView;
private EditText edtId;
private EditText edtPass;
private Button btnlog;
private Button btnfindId;
private Button btnfindPass;
private Button btnJoin;
private String mID = "";
private String mPASS = "";
private String id = "";
private String pass = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/NanumBarunGothicBold.ttf");
titleTextView = (TextView) findViewById(R.id.title_text);
titleTextView.setTypeface(typeFace);
edtId = (EditText)findViewById(R.id.edt_id);
edtPass = (EditText)findViewById(R.id.edt_pass);
Button btnlog = (Button)findViewById(R.id.btn_login);
Button btnJoin= (Button)findViewById(R.id.btn_join);
Button btnfindId = (Button)findViewById(R.id.btn_find_id);
Button btnfindPass = (Button)findViewById(R.id.btn_find_password);
findViewById(R.id.btn_join).setOnClickListener(this);
findViewById(R.id.btn_login).setOnClickListener(this);
findViewById(R.id.btn_find_id).setOnClickListener(this);
findViewById(R.id.btn_find_password).setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_login: {
id = edtId.getText().toString(); // 사용자 입력된 아이디 값을 id 변수에 넣습니다.
pass = edtPass.getText().toString();// 사용자 입력된 패스워드를 pass변수에 넣습니다.
Log.d("TAG", "22222"+mID);
Log.d("TAG", "22222"+mPASS);
Log.d("TAG", "22222"+id);
Log.d("TAG", "22222"+pass);
GetData task1 = new GetData();
task1.execute(new String[]{"http://192.168.42.219/select.php"});
if(id.equals(mID) && pass.equals(mPASS)){ // 사용자 입력 : id , DB데이터ID : mID 비교
Intent intent = new Intent(LogInActivity.this, MainActivity.class);
}
else
Toast.makeText(this, "비밀번호를 다시 확인해주세요", Toast.LENGTH_SHORT).show();
break;
}
}
}
public class GetData extends AsyncTask<String, Void, Boolean>{
String text = "";
@Override
protected void onPreExecute() {
//dialog.setMessage("데이터를 읽어오는중..");
//dialog.show();
}
@Override
protected Boolean doInBackground(String... urls) {
for(String url1 : urls){
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url1);
HttpResponse response = httpclient.execute(httppost);
Log.d("TAG", "111"+url1);
//
String result = Html.fromHtml(EntityUtils.toString(response.getEntity()).trim()).toString();
onConverter(result);
}
catch(Exception e){
e.printStackTrace();
}
}
return true;
}
private void onConverter(String result) {
String key = "result";
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(result);
if (null != jsonObj) {
JSONArray JsonArray = new JSONArray();
if (jsonObj.has(key)) {
JsonArray = jsonObj.getJSONArray(key);
}
if (null != JsonArray && JsonArray.length() > 0) {
JSONObject ItemjsonObj = JsonArray.getJSONObject(0);
mID = ItemjsonObj.getString("id");
mPASS = ItemjsonObj.getString("password");
}
}
} catch(Exception e) {
Log.e("TAG", "LOGINERROR ::: " + e.toString());
}
}
}
protected void onPostExecute(Boolean result){
if(result == true){
Log.d("TAG", "1111111"+result);
Log.d("TAG", "1111111"+mID);
Log.d("TAG", "1111111"+mPASS);
}
}
}
------------------------------------------ select.php --------------------------------------
<?php
include_once('connection.php');
$query = "SELECT id, password FROM register";
$result = mysql_query($query) or die('Error in query : $query' . mysql_error());
while($row = mysql_fetch_assoc($result)){
$output[] = $row;
}
print (json_encode($output));
?>
------------------------------------------ connection.php--------------------------------
<?php
$server_name = '127.0.0.1';
$user_name = 'root';
$password = '1111';
$con = mysql_connect($server_name, $user_name, $password) or die('Server Error: ' . mysql_error());
// 데이터베이스 셀렉트
$database_name = 'db1';
mysql_select_db($database_name) or die ('DB Error : Unable to select db');
mysql_query("SET NAMES 'utf8'");
?>