Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.65 KB | None | 0 0
  1. package com.abc.ukm_project1;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.net.Uri;
  8. import android.os.AsyncTask;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.ArrayAdapter;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16. import android.widget.Toast;
  17.  
  18. import org.json.JSONException;
  19. import org.json.JSONObject;
  20.  
  21. import java.util.ArrayList;
  22.  
  23. /**
  24. * Created by Cisco on 10/2/2017.
  25. */
  26.  
  27. public class CustomRowAdapter extends ArrayAdapter<users>
  28. {
  29. private final Context context;
  30. private final ArrayList<users> users_list;
  31. private ProgressDialog pDialog;
  32. JSONParser jsonParser = new JSONParser();
  33. private static final String url_login = "http://10.3.68.191/server/resetusers.php";
  34.  
  35. public CustomRowAdapter(Context context, ArrayList<users> users_list)
  36. {
  37. super(context,R.layout.rowlayout,users_list);
  38. this.context=context;
  39. this.users_list=users_list;
  40. }
  41. public View getView(int position, View convertView, ViewGroup parent)
  42. {
  43. LayoutInflater inflater = (LayoutInflater) context
  44. .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  45. View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
  46. final TextView tfullname = (TextView) rowView.findViewById(R.id.tfullname);
  47. final TextView tusername = (TextView) rowView.findViewById(R.id.tusername);
  48. users obj = users_list.get(position);
  49. tfullname.setText(obj.fullname);
  50. tusername.setText(obj.username);
  51. Button btnreset = (Button) rowView.findViewById(R.id.btnreset);
  52. btnreset.setOnClickListener(new View.OnClickListener() {
  53. public void onClick(View v) {
  54. new resetusers().execute(tusername.getText().toString(),null,null);
  55. }
  56. });
  57. return rowView;
  58. }
  59. class resetusers extends AsyncTask<String, String, String>
  60. {
  61. protected void onPreExecute()
  62. {
  63. super.onPreExecute();
  64. pDialog = new ProgressDialog(context);
  65. pDialog.setMessage("Reseting user password. Please wait...");
  66. pDialog.setIndeterminate(false);
  67. pDialog.setCancelable(true);
  68. pDialog.show();
  69. }
  70. protected String doInBackground(String... params)
  71. {
  72. int success;
  73.  
  74. try
  75. {
  76. Uri.Builder builder = new Uri.Builder()
  77. .appendQueryParameter("username", params[0]);
  78. String query = builder.build().getEncodedQuery();
  79. JSONObject json = jsonParser.makeHttpRequest(url_login, query);
  80. if(json !=null)
  81. {
  82. Log.d("resettinguser", json.toString());
  83. success = json.getInt("result");
  84. if (success == 1)
  85. {
  86. ((Activity) context).runOnUiThread(new Runnable()
  87. {
  88. public void run()
  89. {
  90. Toast.makeText(context, "Reset is success", Toast.LENGTH_LONG).show();
  91. }
  92. });
  93. }
  94. else
  95. {
  96. ((Activity)context).runOnUiThread(new Runnable()
  97. {
  98. public void run()
  99. {
  100. Toast.makeText(context, "Reset failed", Toast.LENGTH_LONG).show();
  101. }
  102. });
  103. }
  104. }
  105. else
  106. {
  107. ((Activity)context).runOnUiThread(new Runnable()
  108. {
  109. public void run()
  110. {
  111. Toast.makeText(context,"Unable to contact server",Toast.LENGTH_LONG).show();
  112.  
  113. }
  114. });
  115. return null;
  116. }
  117. }
  118. catch (JSONException e)
  119. {
  120. e.printStackTrace();
  121. }
  122. return null;
  123. }
  124. protected void onPostExecute(String file_url)
  125. {
  126. pDialog.dismiss();
  127. }
  128. }
  129. }
  130.  
  131.  
  132.  
  133.  
  134. package com.abc.ukm_project1;
  135.  
  136. import android.app.Activity;
  137. import android.app.ProgressDialog;
  138. import android.os.AsyncTask;
  139. import android.os.Bundle;
  140. import android.widget.ArrayAdapter;
  141. import android.widget.ListView;
  142. import android.widget.Toast;
  143.  
  144. import org.json.JSONObject;
  145.  
  146. import java.util.ArrayList;
  147.  
  148. /**
  149. * Created by Cisco on 9/26/2017.
  150. */
  151.  
  152. public class usersactivity extends Activity {
  153.  
  154. ListView listview1;
  155. private ProgressDialog pDialog;
  156. JSONParser jsonParser = new JSONParser();
  157. private static final String url_login = "http://10.3.68.191/server/getusers.php";
  158.  
  159. public void onCreate(Bundle b) {
  160. super.onCreate(b);
  161. setContentView(R.layout.userslayout);
  162.  
  163. listview1 = (ListView) findViewById(R.id.listview1);
  164. new getlistview().execute();
  165.  
  166. }
  167.  
  168. class getlistview extends AsyncTask<String, String, String> {
  169. protected void onPreExecute() {
  170. super.onPreExecute();
  171. pDialog = new ProgressDialog(usersactivity.this);
  172. pDialog.setMessage("Loading ListView. Please wait...");
  173. pDialog.setIndeterminate(false);
  174. pDialog.setCancelable(true);
  175. pDialog.show();
  176. }
  177.  
  178. protected String doInBackground(String... params) {
  179. try {
  180. JSONObject json = jsonParser.makeHttpRequest(url_login, "");
  181. final ArrayList<users> users_list = new ArrayList<>();
  182. if (json != null) {
  183.  
  184. int k = 0;
  185. while (k < json.getJSONArray("fullname").length()) {
  186. users obj = new users();
  187. obj.username = json.getJSONArray("username").getString(k);
  188. obj.fullname = json.getJSONArray("fullname").getString(k);
  189. users_list.add(obj);
  190. k++;
  191. }
  192. usersactivity.this.runOnUiThread(new Runnable() {
  193. public void run() {
  194. //String[] data = users.toArray(new String[users.size()]);
  195. CustomRowAdapter adapter = new CustomRowAdapter(usersactivity.this, users_list);
  196. ListView listView = (ListView) findViewById(R.id.listview1);
  197. listView.setAdapter(adapter);
  198.  
  199. }
  200. });
  201. } else {
  202. usersactivity.this.runOnUiThread(new Runnable() {
  203. public void run() {
  204. Toast.makeText(usersactivity.this, "Unable to contact server", Toast.LENGTH_LONG).show();
  205. }
  206. });
  207. return null;
  208. }
  209. } catch (Exception e) {
  210. e.printStackTrace();
  211. }
  212. return null;
  213. }
  214.  
  215. protected void onPostExecute(String file_url) {
  216. pDialog.dismiss();
  217. }
  218. }
  219. }
  220.  
  221. package com.abc.ukm_project1;
  222.  
  223. /**
  224. * Created by Cisco on 10/3/2017.
  225. */
  226.  
  227. public class users {
  228.  
  229. public String username;
  230. public String fullname;
  231. }
  232.  
  233.  
  234.  
  235.  
  236.  
  237. <?php
  238. include "newdb.php";
  239. $dbcon=connect_db();
  240. ?>
  241. <?php
  242. $flag=0;
  243. $statement="SELECT username,fullname FROM users";
  244. $result=mysqli_query($dbcon,$statement);
  245. $myObj=new \stdClass();
  246. $i=0;
  247. while($row=mysqli_fetch_array($result) )
  248. {
  249. $myObj->username[$i] = $row['username'];
  250. $myObj->fullname[$i] = $row['fullname'];
  251. $i++;
  252. }
  253. close_db();
  254. $myJSON = json_encode($myObj);
  255. echo $myJSON;
  256. exit();
  257. ?>
  258.  
  259.  
  260.  
  261. <?php
  262. include "newdb.php";
  263. $dbcon=connect_db();
  264. ?>
  265. <?php
  266. $username=$_POST["username"];
  267. //$username='selva';
  268. //$password='password';
  269. $flag=0;
  270. $statement="UPDATE users SET password = '12345' where username = '$username'";
  271. //echo $statement;
  272. //exit();
  273. if (mysqli_query($dbcon, $statement))
  274. {
  275. $myObj=new \stdClass();
  276. $myObj->result = "1";
  277. $myJSON = json_encode($myObj);
  278. echo $myJSON;
  279. exit();
  280. }
  281. else
  282. {
  283. $myObj=new \stdClass();
  284. $myObj->result = "2";
  285. $myJSON = json_encode($myObj);
  286. echo $myJSON;
  287. exit();
  288. }
  289. close_db();
  290. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement