Guest User

Untitled

a guest
Dec 16th, 2017
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. public class LoginActivity extends Activity {
  2. EditText ET_NAME, ET_PASS;
  3. String login_name, login_pass;
  4.  
  5. @Override
  6. protected void onCreate(@Nullable Bundle savedState) {
  7. super.onCreate(savedState);
  8. setContentView(R.layout.activity_main);
  9.  
  10. ET_NAME = findViewById(R.id.user_name);
  11. ET_PASS = findViewById(R.id.user_pass);
  12. }
  13.  
  14. public void userLogin(View view) {
  15. login_name = ET_NAME.getText().toString();
  16. login_pass = ET_PASS.getText().toString();
  17. String method = "login";
  18. BackgroundTask backgroundTask = new BackgroundTask(this);
  19. backgroundTask.execute(method, login_name, login_pass);
  20. }
  21.  
  22. public void userReg(View view) {
  23. startActivity(new Intent(this, RegisterActivity.class));
  24. }
  25. }
  26.  
  27. public class BackgroundTask extends AsyncTask<String, Void, String> {
  28. AlertDialog alertDialog;
  29. Context ctx;
  30.  
  31. public BackgroundTask(Context ctx) {
  32. this.ctx = ctx;
  33. }
  34.  
  35. @Override
  36. protected void onPreExecute() {
  37. alertDialog = new AlertDialog.Builder(ctx).create();
  38. alertDialog.setTitle("Login information");
  39. }
  40.  
  41. @Override
  42. protected String doInBackground(String... params) {
  43. String reg_url = "http://192.168.111.1/webapp/register.php";
  44. String login_url = "http://192.168.111.1/webapp/login.php";
  45. String method = params[0];
  46. if (method.equals("register")) {
  47. String name = params[1];
  48. String user_name = params[2];
  49. String user_pass = params[3];
  50.  
  51. try {
  52. URL url = new URL(reg_url);
  53. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  54. httpURLConnection.setRequestMethod("POST");
  55. httpURLConnection.setDoOutput(true);
  56. OutputStream OS = httpURLConnection.getOutputStream();
  57. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  58. String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
  59. URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
  60. URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
  61. bufferedWriter.write(data);
  62. bufferedWriter.flush();
  63. bufferedWriter.close();
  64. OS.close();
  65. InputStream IS = httpURLConnection.getInputStream();
  66. IS.close();
  67. return "Register Success...";
  68. } catch (MalformedURLException e) {
  69. e.printStackTrace();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. } else if (method.equals("login")) {
  74. String login_name = params[1];
  75. String login_pass = params[2];
  76. try {
  77. URL url = new URL(login_url);
  78. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  79. httpURLConnection.setRequestMethod("POST");
  80. httpURLConnection.setDoOutput(true);
  81. httpURLConnection.setDoInput(true);
  82. OutputStream outputStream = httpURLConnection.getOutputStream();
  83. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
  84. String data = URLEncoder.encode("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" +
  85. URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "UTF-8");
  86. bufferedWriter.write(data);
  87. bufferedWriter.flush();
  88. bufferedWriter.close();
  89. outputStream.close();
  90.  
  91. InputStream inputStream = httpURLConnection.getInputStream();
  92. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
  93. String response = "";
  94. String line = "";
  95. while ((line = bufferedReader.readLine()) != null) {
  96. response += line;
  97. }
  98. bufferedReader.close();
  99. inputStream.close();
  100. httpURLConnection.disconnect();
  101. return response;
  102. } catch (MalformedURLException e) {
  103. e.printStackTrace();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. return null;
  109. }
  110.  
  111. @Override
  112. protected void onProgressUpdate(Void... values) {
  113. super.onProgressUpdate(values);
  114. }
  115.  
  116. @Override
  117. protected void onPostExecute(String result) {
  118. if (result.equals("Register Success..."))
  119. Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();
  120. else {
  121. alertDialog.setMessage(result);
  122. alertDialog.show();
  123. }
  124. }
  125. }
  126.  
  127. public class MainActivity extends AppCompatActivity {
  128. String JSON_STRING;
  129. String json_string;
  130.  
  131.  
  132. @Override
  133. protected void onCreate(Bundle savedInstanceState) {
  134. super.onCreate(savedInstanceState);
  135. setContentView(R.layout.activity_main);
  136. }
  137.  
  138. public void getJSON(View view) {
  139. BackgroundTask backgroundTask = new BackgroundTask(this);
  140. backgroundTask.execute();
  141. }
  142.  
  143. public void parseJSON(View view) {
  144. if (json_string == null) {
  145. Toast.makeText(getApplicationContext(), "First Get JSON", Toast.LENGTH_SHORT).show();
  146. } else {
  147. Intent intent = new Intent(getApplicationContext(), DisplayListView.class);
  148. intent.putExtra("json_data", json_string);
  149. startActivity(intent);
  150. }
  151. }
  152.  
  153. class BackgroundTask extends AsyncTask<Void, Void, String> {
  154. String json_url;
  155. Context context;
  156.  
  157. public BackgroundTask(Context context) {
  158. this.context = context;
  159. }
  160.  
  161. @Override
  162. protected String doInBackground(Void... aVoid) {
  163. try {
  164. URL url = new URL(json_url);
  165. HttpURLConnection hucon = (HttpURLConnection) url.openConnection();
  166. InputStream is = hucon.getInputStream();
  167. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  168. StringBuilder sb = new StringBuilder();
  169. while ((JSON_STRING = br.readLine()) != null) {
  170. sb.append(JSON_STRING + "n");
  171. }
  172.  
  173. br.close();
  174. is.close();
  175. hucon.disconnect();
  176. return sb.toString().trim();
  177. } catch (MalformedURLException e) {
  178. e.printStackTrace();
  179. } catch (IOException e) {
  180. e.printStackTrace();
  181. }
  182. return null;
  183. }
  184.  
  185. @Override
  186. protected void onPreExecute() {
  187. json_url = "http://192.168.111.1/jsonparsing/json_get_data.php";
  188. }
  189.  
  190. @Override
  191. protected void onProgressUpdate(Void... values) {
  192. super.onProgressUpdate(values);
  193. }
  194.  
  195. @Override
  196. protected void onPostExecute(String result) {
  197. TextView textView = (TextView) findViewById(R.id.tv_json_value);
  198. textView.setText(result);
  199. json_string = result;
  200. }
  201. }
  202. }
  203.  
  204. <? php
  205.  
  206. $host = "localhost";
  207. $user="root";
  208. $password="";
  209. $db="app_db";
  210.  
  211. $con=mysqli_connect($host,$user,$password,$db);
  212.  
  213.  
  214. $sql="select * from user;";
  215.  
  216. $result=mysqli_query($con,$sql);
  217. $response=array();
  218.  
  219. while($row=mysqli_fetch_array($result)){
  220. array_push($response,array('full_name'=>$row[1],'user_name'=>$row[2],'email'=>$row[3],'phone'=>$row[4]));
  221. }
  222.  
  223. mysqli_close($con);
  224. echo json_encode(array("server_response"=>$response));
  225. ?>
Add Comment
Please, Sign In to add comment