package org.android.test1;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
public class LoginAct extends Activity {
Button Login;
EditText WorkerNum;
CheckBox IdSave;
CheckBox AutoLogin;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
Login = (Button) findViewById(R.id.login);
WorkerNum = (EditText) findViewById(R.id.workernum);
IdSave = (CheckBox) findViewById(R.id.idsave);
AutoLogin = (CheckBox) findViewById(R.id.autologin);
tv = (TextView) findViewById(R.id.error);
Login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(LoginAct.this, "",
"사원번호를 확인중입니다.", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
}
});
}
void login() {
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost(
"http://192.168.123.145:8079/Test4/test13.jsp");
Gson gson = new Gson();
nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("ID", WorkerNum.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() {
public void run() {
tv.setText("Response from JSP : " + response);
dialog.dismiss();
}
});
if (response.equalsIgnoreCase("확인되었습니다.")) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(LoginAct.this, "성공", Toast.LENGTH_SHORT)
.show();
}
});
startActivity(new Intent(LoginAct.this, UserPage.class));
} else {
showAlert();
}
} catch (Exception e) {
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert() {//
LoginAct.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(
LoginAct.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}