Guest User

Untitled

a guest
Feb 22nd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity {
  2. String username ;
  3. String password;
  4.  
  5. public void doLgin(View view) {
  6. if(Build.VERSION.SDK_INT >= 10){
  7. StrictMode.ThreadPolicy policy = StrictMode.ThreadPolicy.LAX;
  8. StrictMode.setThreadPolicy(policy);
  9. }
  10.  
  11. username = ((EditText)findViewById(R.id.editTextUsername)).getText().toString();
  12. password = ((EditText)findViewById(R.id.editTextPassword)).getText().toString();
  13.  
  14. try {
  15.  
  16. HttpClient httpClient = new DefaultHttpClient();
  17. HttpGet httpPost = new HttpGet("http://182.18.163.39/m/login.php?username=" + username + "&key=" + password);
  18. HttpResponse response = httpClient.execute(httpPost);
  19. HttpEntity entity = response.getEntity();
  20.  
  21. String responseString = EntityUtils.toString(entity);
  22. JSONArray jsonarray = new JSONArray(responseString);
  23. JSONObject jsonObj = new JSONObject();
  24. jsonObj.put("Result", jsonarray);
  25. String error = jsonObj.getJSONArray("Result").getJSONObject(0).toString();
  26. String errormsg = "{"Error":"Problem with authentication,Please login."}";
  27.  
  28. if (error.equalsIgnoreCase(errormsg)) {
  29. Toast.makeText(getApplicationContext(), "Invalid username or password", Toast.LENGTH_SHORT).show();
  30.  
  31. } else {
  32. // Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
  33. Intent i = new Intent(MainActivity.this, Main2Activity.class);
  34.  
  35. startActivity(i);
  36.  
  37. }
  38. }
  39.  
  40. public class Main2Activity extends AppCompatActivity {
  41.  
  42. MainActivity m = new MainActivity();
  43. String user = m.username;
  44. String pass = m.password;
  45.  
  46. String JSON_URL = "http://182.18.163.39/m/list.php?username=admin&key=admin";
  47. ListView listView;
  48. java.util.List<List> tktList;
  49.  
  50. String link;
  51. Button logoutbt;
  52. Button ubt;
  53.  
  54. @Override
  55. protected void onCreate(Bundle savedInstanceState) {
  56. super.onCreate(savedInstanceState);
  57. setContentView(R.layout.activity_main2);
  58.  
  59. logoutbt = (Button)findViewById(R.id.lbt);
  60. logoutbt.setOnClickListener(new View.OnClickListener() {
  61. @Override
  62. public void onClick(View view) {
  63. SharedPreferences SM = getSharedPreferences("userrecord", 0);
  64. SharedPreferences.Editor edit = SM.edit();
  65. edit.putBoolean("username", false);
  66. edit.commit();
  67.  
  68. Intent intent = new Intent(Main2Activity.this, MainActivity.class);
  69. startActivity(intent);
  70. finish();
  71. }
  72. });
  73.  
  74. //initializing listview and hero list
  75. listView = (ListView) findViewById(R.id.listView);
  76. tktList = new ArrayList<>();
  77.  
  78. //this method will fetch and parse the data
  79. loadHeroList();
  80. }
  81.  
  82. private void loadHeroList() {
  83. //getting the progressbar
  84. final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  85.  
  86. //making the progressbar visible
  87. progressBar.setVisibility(View.VISIBLE);
  88.  
  89. //creating a string request to send request to the url
  90. StringRequest stringRequest = new StringRequest(Request.Method.GET, JSON_URL,
  91. new Response.Listener<String>() {
  92. @Override
  93. public void onResponse(String response) {
  94. //hiding the progressbar after completion
  95. progressBar.setVisibility(View.INVISIBLE);
  96.  
  97. try {
  98.  
  99. JSONArray jsonarray = new JSONArray(response);
  100. for (int i = 0; i < jsonarray.length(); i++) {
  101. JSONObject jsonobject = jsonarray.getJSONObject(i);
  102. String name = jsonobject.getString("Sno");
  103. String Tktid = jsonobject.getString("TKTID");
  104. link = jsonobject.getString("Link");
  105.  
  106. List list = new List(jsonobject.getString("Sno"), jsonobject.getString("TKTID"),jsonobject.getString("Link"));
  107. tktList.add(list);
  108.  
  109. Log.i("website content", name);
  110. Log.i("website content", Tktid);
  111. Log.i("website content", link);
  112.  
  113. }
  114.  
  115. //creating custom adapter object
  116. ListViewAdapter adapter = new ListViewAdapter(tktList, getApplicationContext());
  117.  
  118.  
  119. //adding the adapter to listview
  120. listView.setAdapter(adapter);
  121.  
  122. } catch (JSONException e) {
  123. e.printStackTrace();
  124. }
  125. }
  126. },
  127. new Response.ErrorListener() {
  128. @Override
  129. public void onErrorResponse(VolleyError error) {
  130. //displaying the error in toast if occurrs
  131. Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
  132. }
  133. });
  134.  
  135. //creating a request queue
  136. com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(this);
  137.  
  138. //adding the string request to request queue
  139. requestQueue.add(stringRequest);
  140. }
  141. }
  142.  
  143. MainActivity m = new MainActivity();
  144. String user = m.username;
  145. String pass = m.password;
Add Comment
Please, Sign In to add comment