Advertisement
annisa27

ini login baru

Mar 13th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. package com.dish.menu;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.Editor;
  9.  
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. import org.apache.http.NameValuePair;
  14. import org.apache.http.message.BasicNameValuePair;
  15. import org.json.JSONException;
  16. import org.json.JSONObject;
  17.  
  18. import com.dish.sharedpreferences.SharedPreference;
  19. import com.dish.nama.Bayar;
  20. import com.dish.nama.Detail;
  21. import com.dish.nama.Edit;
  22. import com.dish.nama.JSONParser;
  23. import com.dish.nama.MainActivity;
  24. import com.dish.nama.Next;
  25. import com.dish.nama.Print;
  26. import com.dish.nama.R;
  27.  
  28. import android.app.Activity;
  29. import android.app.ProgressDialog;
  30. import android.content.Intent;
  31. import android.view.Menu;
  32. import android.view.View;
  33. import android.view.View.OnClickListener;
  34. import android.widget.Button;
  35. import android.widget.EditText;
  36. import android.widget.TextView;
  37. import android.widget.Toast;
  38.  
  39. public class LoginActivity extends Activity implements View.OnClickListener{
  40. // User name
  41. private EditText et_Username;
  42. // Password
  43. private EditText et_Password;
  44. // Sign In
  45. private Button bt_SignIn;
  46. // Message
  47. //private TextView tv_Message;
  48. String username;
  49. String password;
  50. String id;
  51. String hak_akses;
  52. String uname;
  53. String kode;
  54. String pwd;
  55. TextView login;
  56. // String [] data;
  57. private static String url_cari = "http://cavendish.esy.es/pdam/db_login.php";
  58.  
  59.  
  60. JSONParser jsonParser = new JSONParser();
  61. private ProgressDialog pDialog;
  62.  
  63. //sharedpreference
  64. private SharedPreference sharedPreference;
  65. Activity context = this;
  66. Activity context2 = this;
  67. Activity context3 = this;
  68. Activity context4 = this;
  69. private String text;
  70. private String text2;
  71. private String text3;
  72. private Boolean status_login;
  73.  
  74. private static final String TAG_Loket = "Loket";
  75.  
  76. @Override
  77. protected void onCreate(Bundle savedInstanceState) {
  78. super.onCreate(savedInstanceState);
  79. setContentView(R.layout.activity_login);
  80.  
  81. //sharedpreferences
  82. sharedPreference = new SharedPreference();
  83.  
  84. // Initialization
  85. et_Username = (EditText) findViewById(R.id.usernameET);
  86. et_Password = (EditText) findViewById(R.id.passwordET);
  87. bt_SignIn = (Button) findViewById(R.id.loginBtn);
  88.  
  89. bt_SignIn.setOnClickListener(this);
  90. }
  91.  
  92. class login extends AsyncTask<String, String, String> {
  93. /**
  94. * Menampilkan Progress Dialog
  95. */
  96. @Override
  97. protected void onPreExecute() {
  98. super.onPreExecute();
  99. pDialog = new ProgressDialog(LoginActivity.this);
  100. pDialog.setMessage("Autentikasi . . . ");
  101. pDialog.setIndeterminate(false);
  102. pDialog.setCancelable(true);
  103. pDialog.show();
  104. }
  105.  
  106. /**
  107. * Menyimpan Data
  108. */
  109. protected String doInBackground(String... args) {
  110. String username = et_Username.getText().toString();
  111. String password = et_Password.getText().toString();
  112.  
  113. // String u = data[0];
  114. // String p = data[1];
  115. //
  116. try {
  117. ArrayList localArrayList = new ArrayList();
  118. localArrayList.add(new BasicNameValuePair("Username", LoginActivity.this.username));
  119. localArrayList.add(new BasicNameValuePair("Password", LoginActivity.this.password));
  120. System.out.println(localArrayList);
  121. JSONObject localJSONObject1 = LoginActivity.this.jsonParser.makeHttpRequest(LoginActivity.url_cari, "GET",
  122. localArrayList);
  123. Log.d("Hasil", localJSONObject1.toString());
  124. if (localJSONObject1.getInt("success") == 1) {
  125. JSONObject localJSONObject2 = localJSONObject1.getJSONArray("Hasil").getJSONObject(0);
  126. LoginActivity.this.id = localJSONObject2.getString("ID");
  127. LoginActivity.this.uname = localJSONObject2.getString("Username");
  128. LoginActivity.this.pwd = localJSONObject2.getString("Password");
  129. LoginActivity.this.hak_akses = localJSONObject2.getString("HakAkses");
  130. LoginActivity.this.kode = localJSONObject2.getString("Kode");
  131.  
  132. } else {
  133. //Toast.makeText(Bayar.this.getApplicationContext(), "Pelanggan Telah Membayar", 0).show();
  134. }
  135. runOnUiThread(new Runnable() {
  136.  
  137. public void run() {
  138.  
  139. Toast.makeText(LoginActivity.this.getApplicationContext(), "doinbackground "+uname, 0).show();
  140.  
  141. }
  142. });
  143. } catch (JSONException localJSONException) {
  144. localJSONException.printStackTrace();
  145. }
  146. return null;
  147. }
  148.  
  149. /**
  150. * Setalah selesai dialog menghilang
  151. **/
  152. protected void onPostExecute(String file_url) {
  153. // menutup progres dialog jika sudah selesai
  154. pDialog.dismiss();
  155. }
  156. }
  157.  
  158. @Override
  159. public void onClick(View v) {
  160. // TODO Auto-generated method stub
  161. // Stores User name
  162. username = String.valueOf(et_Username.getText());
  163. // Stores Password
  164. password = String.valueOf(et_Password.getText());
  165. // String [] data = {username,password};
  166. runOnUiThread(new Runnable() {
  167.  
  168. public void run() {
  169.  
  170. Toast.makeText(LoginActivity.this.getApplicationContext(), "Username awal: "+username, 0).show();
  171.  
  172. }
  173. });
  174.  
  175. if(text==null){
  176. login qwerty = new login();
  177. qwerty.execute();
  178.  
  179. // Validates the User name and Password for admin, admin
  180. if (username.equals(this.uname) && password.equals(this.pwd)) {
  181.  
  182. // Save the text in SharedPreference
  183. text = this.kode;
  184. text2 = this.hak_akses;
  185. text3 = this.username;
  186. sharedPreference.save(context, text);
  187. sharedPreference.save(context2, text2);
  188. sharedPreference.save(context3, text3);
  189.  
  190. runOnUiThread(new Runnable() {
  191.  
  192. public void run() {
  193.  
  194. Toast.makeText(LoginActivity.this.getApplicationContext(), "Login Berhasil: "+text3, 0).show();
  195.  
  196. }
  197. });
  198.  
  199. Intent localIntent = new Intent(this, Next.class);
  200. startActivity(localIntent);
  201. } else {
  202. runOnUiThread(new Runnable() {
  203.  
  204. public void run() {
  205.  
  206. Toast.makeText(LoginActivity.this.getApplicationContext(), "Layar "+username+" database "+uname, 0).show();
  207.  
  208. }
  209. });
  210. runOnUiThread(new Runnable() {
  211.  
  212. public void run() {
  213.  
  214. Toast.makeText(LoginActivity.this.getApplicationContext(), "Login Gagal "+text3, 0).show();
  215.  
  216. }
  217. });
  218. }
  219. }else{
  220. sharedPreference.clearSharedPreference(context);
  221. sharedPreference.clearSharedPreference(context2);
  222. sharedPreference.clearSharedPreference(context3);
  223. }
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement