Advertisement
Guest User

Untitled

a guest
Mar 19th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.45 KB | None | 0 0
  1. package com.example.yuliana.accessws01;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.ArrayAdapter;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.ListView;
  10. import android.widget.Toast;
  11.  
  12. import com.loopj.android.http.AsyncHttpClient;
  13. import com.loopj.android.http.AsyncHttpResponseHandler;
  14. import com.loopj.android.http.RequestParams;
  15.  
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import java.util.ArrayList;
  21.  
  22. public class MainActivity extends AppCompatActivity {
  23. Button btnSearch,btnLogin,btnRegister;
  24. EditText edtUsername,edtPassword,edtNama,edtGaji;
  25. String tombol;
  26. ListView lv;
  27.  
  28. @Override
  29. protected void onCreate(Bundle savedInstanceState) {
  30. super.onCreate(savedInstanceState);
  31. setContentView(R.layout.activity_main);
  32. btnSearch = (Button) findViewById(R.id.btnSearch);
  33. btnLogin = (Button) findViewById(R.id.btnLogin);
  34. btnRegister = (Button)findViewById(R.id.btnReg);
  35. edtPassword = (EditText)findViewById(R.id.txtpassword);
  36. edtUsername = (EditText)findViewById(R.id.txtusername);
  37. edtNama = (EditText)findViewById(R.id.txtNama);
  38. edtGaji = (EditText)findViewById(R.id.txtGaji);
  39. lv = (ListView)findViewById(R.id.listView);
  40. btnRegister.setOnClickListener(new View.OnClickListener() {
  41. @Override
  42. public void onClick(View v) {
  43. RequestParams param = new RequestParams();
  44. param.put("tusername",edtUsername.getText().toString());
  45. param.put("tpasswd", edtPassword.getText().toString());
  46. param.put("tnama",edtNama.getText().toString());
  47. param.put("tgaji",edtGaji.getText().toString());
  48. tombol = "register";
  49. panggilWS("post",param);
  50. }
  51. });
  52. btnLogin.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. RequestParams param = new RequestParams();
  56. param.put("tusername",edtUsername.getText().toString());
  57. param.put("tpasswd", edtPassword.getText().toString());
  58. tombol = "login";
  59. panggilWS("post",param);
  60. }
  61. });
  62. btnSearch.setOnClickListener(new View.OnClickListener() {
  63. @Override
  64. public void onClick(View v) {
  65. RequestParams param = new RequestParams();
  66. param.put("tusername",edtUsername.getText().toString());
  67. tombol = "search";
  68. panggilWS("post",param);
  69. }
  70. });
  71. }
  72. public void panggilWS(String jenis, RequestParams param){
  73. AsyncHttpClient client = new AsyncHttpClient();
  74. if(jenis.equalsIgnoreCase("post")) {
  75. String command = "";
  76. if(tombol.equalsIgnoreCase("register"))
  77. { command = "http://10.10.18.224/prac01/index.php/Myservice/insertmember"; }
  78. else if(tombol.equalsIgnoreCase("login"))
  79. { command = "http://10.10.18.224/prac01/index.php/Myservice/checklogin"; }
  80. else if(tombol.equalsIgnoreCase("search"))
  81. { command = "http://10.10.18.224/prac01/index.php/Myservice/selectmember"; }
  82.  
  83. client.post(command,param,
  84. new AsyncHttpResponseHandler()
  85. {
  86. @Override
  87. public void onSuccess(String Response)
  88. {
  89. if(tombol.equalsIgnoreCase("register")) {
  90. Toast.makeText(getApplicationContext(),
  91. "Register Sukses", Toast.LENGTH_LONG).show();
  92. }
  93. if(tombol.equalsIgnoreCase("login")) {
  94. if(Response.equalsIgnoreCase("invalid")) {
  95. Toast.makeText(getApplicationContext(),
  96. "Username Password Anda Salah", Toast.LENGTH_LONG).show();
  97. }
  98. else {
  99. Toast.makeText(getApplicationContext(),
  100. "Selamat Datang", Toast.LENGTH_LONG).show();
  101. }
  102. }
  103. if(tombol.equalsIgnoreCase("search")) {
  104. ArrayList<String> arr = new ArrayList<>();
  105. JSONArray jArray = null;
  106. try {
  107. jArray = new JSONArray(Response);
  108. int panjang = jArray.length();
  109. for(int i = 0; i < jArray.length(); i+=1)
  110. {
  111. JSONObject jsondata = jArray.getJSONObject(i);
  112. arr.add(jsondata.getString("username"));
  113. }
  114. ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,arr);
  115. lv.setAdapter(adapter);
  116. } catch (JSONException e) {
  117. e.printStackTrace();
  118. }
  119. }
  120.  
  121. }
  122. @Override
  123. public void onFailure(int statusCode, Throwable error, String content)
  124. {
  125. if (statusCode == 404) {
  126. Toast.makeText(getApplicationContext(),
  127. "Requested resource not found", Toast.LENGTH_LONG).show();
  128. }
  129. else if (statusCode == 500) {
  130. Toast.makeText(getApplicationContext(),
  131. "Something went wrong at server end", Toast.LENGTH_LONG).show();
  132. }
  133. else {
  134. Toast.makeText(getApplicationContext(),
  135. "Unexpected Error occcured! Might be not be connected to Internet",
  136. Toast.LENGTH_LONG).show();
  137. }
  138. }
  139. }
  140. );
  141. }
  142. }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement