Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.08 KB | None | 0 0
  1. package com.abc.intech1;
  2.  
  3. import android.app.Activity;
  4. import android.app.ProgressDialog;
  5. import android.content.Intent;
  6. import android.net.Uri;
  7. import android.os.AsyncTask;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import org.json.JSONException;
  17. import org.json.JSONObject;
  18.  
  19. /**
  20. * Created by selva on 3/7/2018.
  21. */
  22.  
  23. public class loginactivity extends Activity
  24. {
  25. Button btnlogin;
  26. EditText tusername,tpassword;
  27. TextView msg;
  28. ProgressDialog pDialog;
  29. String username,password;
  30. private static final String url_login = "http://192.168.1.79:8080/webapp1/loginservlet";
  31. JSONParser jsonParser = new JSONParser();
  32. public void onCreate(Bundle b)
  33. {
  34. super.onCreate(b);
  35. setContentView(R.layout.loginlayout);
  36. //mapping
  37. btnlogin=findViewById(R.id.btnlogin);
  38. tusername=findViewById(R.id.tusername);
  39. tpassword=findViewById(R.id.tpassword);
  40. msg=findViewById(R.id.msg);
  41.  
  42. btnlogin.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View view) {
  45. username=tusername.getText().toString();
  46. password=tpassword.getText().toString();
  47. if(username.length()==0 || password.length()==0)
  48. {
  49. msg.setText("Username or password is missing");
  50. }
  51. else
  52. {
  53. username=tusername.getText().toString();
  54. password=tpassword.getText().toString();
  55. new getlogindetails().execute();
  56.  
  57. }
  58. }
  59. });
  60. }
  61. class getlogindetails extends AsyncTask<String, String, String>
  62. {
  63. protected void onPreExecute()
  64. {
  65. super.onPreExecute();
  66. pDialog = new ProgressDialog(loginactivity.this);
  67. pDialog.setMessage("Logging in. Please wait...");
  68. pDialog.setIndeterminate(false);
  69. pDialog.setCancelable(true);
  70. pDialog.show();
  71. }
  72. protected String doInBackground(String... params)
  73. {
  74. int success;
  75. try
  76. {
  77. Uri.Builder builder = new Uri.Builder()
  78. .appendQueryParameter("username", username)
  79. .appendQueryParameter("password", password);
  80. String query = builder.build().getEncodedQuery();
  81. JSONObject json = jsonParser.makeHttpRequest(url_login, query);
  82. if(json !=null)
  83. {
  84. Log.d("Login", json.toString());
  85. success = json.getInt("result");
  86. if (success == 1)
  87. {
  88. Intent myintent = new Intent(getApplicationContext(), menuactivity.class);
  89. startActivity(myintent);
  90. }
  91. else
  92. {
  93. loginactivity.this.runOnUiThread(new Runnable()
  94. {
  95. public void run()
  96. {
  97. Toast.makeText(getApplicationContext(), "Not Found", Toast.LENGTH_LONG).show();
  98. }
  99. });
  100. }
  101. }
  102. else
  103. {
  104. loginactivity.this.runOnUiThread(new Runnable()
  105. {
  106. public void run()
  107. {
  108. Toast.makeText(loginactivity.this,"Unable to contact server",Toast.LENGTH_LONG).show();
  109.  
  110. }
  111. });
  112. return null;
  113. }
  114. }
  115. catch (JSONException e)
  116. {
  117. e.printStackTrace();
  118. }
  119. return null;
  120. }
  121. protected void onPostExecute(String file_url)
  122. {
  123. pDialog.dismiss();
  124. }
  125. }
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132. package org.webapp.servlets;
  133.  
  134. import java.io.IOException;
  135. import java.io.PrintWriter;
  136. import javax.servlet.Servlet;
  137. import javax.servlet.ServletException;
  138. import javax.servlet.http.HttpServlet;
  139. import javax.servlet.http.HttpServletRequest;
  140. import javax.servlet.http.HttpServletResponse;
  141.  
  142. import org.json.JSONException;
  143. import org.json.JSONObject;
  144. import org.webapp.engine.users;
  145.  
  146. public class loginservlet extends HttpServlet implements Servlet {
  147. private static final long serialVersionUID = 1L;
  148.  
  149. /**
  150. * @see HttpServlet#HttpServlet()
  151. */
  152. public loginservlet() {
  153. super();
  154. // TODO Auto-generated constructor stub
  155. }
  156.  
  157. /**
  158. * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
  159. */
  160. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  161. // TODO Auto-generated method stub
  162. response.getWriter().append("Served at: ").append(request.getContextPath());
  163. }
  164.  
  165. /**
  166. * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
  167. */
  168. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  169. {
  170. PrintWriter out = response.getWriter();
  171. JSONObject json=new JSONObject();
  172. try
  173. {
  174. //response.setContentType("application/json");
  175. response.setContentType("text/html");
  176.  
  177. String username,password;
  178. username=request.getParameter("username");
  179. password=request.getParameter("password");
  180.  
  181. users u=new users();
  182. u.init();
  183. if(u.opencon())
  184. {
  185.  
  186. if(u.validate(username, password))
  187. {
  188. json.put("result", 1);
  189. }
  190. else
  191. {
  192. json.put("result", 2);
  193. }
  194. }
  195. else
  196. {
  197. json.put("result", 2);
  198. }
  199. }
  200. catch(JSONException e)
  201. {
  202.  
  203. }
  204. out.flush();
  205. out.print(json);
  206. }
  207.  
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement