Advertisement
Guest User

Untitled

a guest
Aug 6th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. public class MainActivity extends
  2. AppCompatActivity {
  3.  
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8.  
  9. final EditText id = (EditText) findViewById(R.id.loginid);
  10. final EditText pass = (EditText) findViewById(R.id.loginpass);
  11.  
  12. Button b1 = (Button) findViewById(R.id.buttonlogin);
  13. b1.setOnClickListener(new View.OnClickListener() {
  14. @Override
  15. public void onClick(View view) {
  16. String username = id.getText().toString();
  17. String password = pass.getText().toString();
  18. if (isNetworkAvailable()) {
  19. RequestBody formBody = new FormBody.Builder()
  20. .add("username", username)
  21. .add("password", password)
  22. .build();
  23.  
  24.  
  25. try {
  26. post(ConstantValues.BASE_URL, formBody, new Callback() {
  27. @Override
  28. public void onFailure(Call call, IOException e) {
  29. Log.e("JSONDemo", "IOException", e);
  30. }
  31.  
  32. @Override
  33. public void onResponse(Call call, Response response) throws IOException {
  34. String res = response.body().string();
  35.  
  36. Log.e("res", " " + res);
  37. try {
  38.  
  39. JSONArray jsonarr = new JSONArray(res);
  40.  
  41. for (int i = 0; i < jsonarr.length(); i++) {
  42.  
  43.  
  44. JSONObject jsonobj = jsonarr.getJSONObject(i);
  45. final String namee = jsonobj.getString("FIRST_NAME");
  46.  
  47.  
  48.  
  49.  
  50. Log.e("name", " " + namee);
  51.  
  52. }
  53. runOnUiThread(new Runnable() {
  54. @Override
  55. public void run() {
  56. // you can access all the UI componenet
  57.  
  58.  
  59. }
  60. });
  61.  
  62.  
  63. } catch (Exception e) {
  64. Log.e("JSONDemo", "onResponse", e);
  65.  
  66.  
  67. }
  68. }
  69. });
  70. } catch (Exception e) {
  71. Log.e("JSONDemo", "Post Exception", e);
  72. }
  73.  
  74.  
  75. }
  76. }
  77. });
  78. }
  79.  
  80.  
  81. private boolean isNetworkAvailable() {
  82. ConnectivityManager connectivityManager
  83. = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
  84. NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
  85. return activeNetworkInfo != null && activeNetworkInfo.isConnected();
  86. }
  87.  
  88.  
  89. private final OkHttpClient client = new OkHttpClient();
  90.  
  91. Call post(String url, RequestBody formBody, Callback callback) throws IOException {
  92.  
  93. Request request = new Request.Builder()
  94. .url(url)
  95. .post(formBody)
  96. .build();
  97.  
  98. Call call = client.newCall(request);
  99. call.enqueue(callback);
  100. return call;
  101. }
  102.  
  103. public void showAlertDialog(String title, String message) {
  104. final AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
  105. builder1.setTitle(title);
  106. builder1.setMessage(message);
  107. builder1.setCancelable(true);
  108. builder1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  109. @Override
  110. public void onClick(DialogInterface dialogInterface, int i) {
  111. dialogInterface.cancel();
  112. }
  113. });
  114. runOnUiThread(new Runnable() {
  115. @Override
  116. public void run() {
  117. builder1.show();
  118.  
  119. }
  120.  
  121. });
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement