Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1. import android.app.Activity;
  2. import android.app.ProgressDialog;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.EditText;
  7. import android.widget.TextView;
  8. import android.widget.Toast;
  9.  
  10. import com.loopj.android.http.AsyncHttpClient;
  11. import com.loopj.android.http.AsyncHttpResponseHandler;
  12. import com.loopj.android.http.RequestParams;
  13.  
  14. import org.json.JSONException;
  15. import org.json.JSONObject;
  16. /**
  17. *
  18. * Register Activity Class
  19. */
  20. public class RegisterActivity extends Activity {
  21. // Progress Dialog Object
  22. ProgressDialog prgDialog;
  23. // Error Msg TextView Object
  24. TextView errorMsg;
  25. // Name Edit View Object
  26. EditText nameET;
  27. // Email Edit View Object
  28. EditText emailET;
  29. // Passwprd Edit View Object
  30. EditText pwdET;
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.register);
  35. // Find Error Msg Text View control by ID
  36. errorMsg = (TextView)findViewById(R.id.register_error);
  37. // Find Name Edit View control by ID
  38. nameET = (EditText)findViewById(R.id.registerName);
  39. // Find Email Edit View control by ID
  40. emailET = (EditText)findViewById(R.id.registerEmail);
  41. // Find Password Edit View control by ID
  42. pwdET = (EditText)findViewById(R.id.registerPassword);
  43. // Instantiate Progress Dialog object
  44. prgDialog = new ProgressDialog(this);
  45. // Set Progress Dialog Text
  46. prgDialog.setMessage("Please wait...");
  47. // Set Cancelable as False
  48. prgDialog.setCancelable(false);
  49. }
  50.  
  51. /**
  52. * Method gets triggered when Register button is clicked
  53. *
  54. * @param view
  55. */
  56. public void registerUser(View view){
  57. // Get NAme ET control value
  58. String name = nameET.getText().toString();
  59. // Get Email ET control value
  60. String email = emailET.getText().toString();
  61. // Get Password ET control value
  62. String password = pwdET.getText().toString();
  63. // Instantiate Http Request Param Object
  64. RequestParams params = new RequestParams();
  65. // When Name Edit View, Email Edit View and Password Edit View have values other than Null
  66. if(Utility.isNotNull(name) && Utility.isNotNull(email) && Utility.isNotNull(password)){
  67. // When Email entered is Valid
  68. if(Utility.validate(email)){
  69. // Put Http parameter name with value of Name Edit View control
  70. params.put("name", name);
  71. // Put Http parameter username with value of Email Edit View control
  72. params.put("username", email);
  73. // Put Http parameter password with value of Password Edit View control
  74. params.put("password", password);
  75. // Invoke RESTful Web Service with Http parameters
  76. invokeWS(params);
  77. }
  78. // When Email is invalid
  79. else{
  80. Toast.makeText(getApplicationContext(), "Please enter valid email", Toast.LENGTH_LONG).show();
  81. }
  82. }
  83. // When any of the Edit View control left blank
  84. else{
  85. Toast.makeText(getApplicationContext(), "Please fill the form, don't leave any field blank", Toast.LENGTH_LONG).show();
  86. }
  87.  
  88. }
  89.  
  90. /**
  91. * Method that performs RESTful webservice invocations
  92. *
  93. * @param params
  94. */
  95. public void invokeWS(RequestParams params){
  96. // Show Progress Dialog
  97. prgDialog.show();
  98. // Make RESTful webservice call using AsyncHttpClient object
  99. AsyncHttpClient client = new AsyncHttpClient();
  100. client.get("http://localhost:8080/useraccount/register/doregister",params ,new AsyncHttpResponseHandler() {
  101. // When the response returned by REST has Http response code '200'
  102. @Override
  103. public void onSuccess(String response) {
  104. // Hide Progress Dialog
  105. prgDialog.hide();
  106. try {
  107. // JSON Object
  108. JSONObject obj = new JSONObject(response);
  109. // When the JSON response has status boolean value assigned with true
  110. if(obj.getBoolean("status")){
  111. // Set Default Values for Edit View controls
  112. setDefaultValues();
  113. // Display successfully registered message using Toast
  114. Toast.makeText(getApplicationContext(), "You are successfully registered!", Toast.LENGTH_LONG).show();
  115. }
  116. // Else display error message
  117. else{
  118. errorMsg.setText(obj.getString("error_msg"));
  119. Toast.makeText(getApplicationContext(), obj.getString("error_msg"), Toast.LENGTH_LONG).show();
  120. }
  121. } catch (JSONException e) {
  122. Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
  123. e.printStackTrace();
  124.  
  125. }
  126. }
  127. // When the response returned by REST has Http response code other than '200'
  128. @Override
  129. public void onFailure(int statusCode, Throwable error,
  130. String content) {
  131. // Hide Progress Dialog
  132. prgDialog.hide();
  133. // When Http response code is '404'
  134. if(statusCode == 404){
  135. Toast.makeText(getApplicationContext(), "Requested resource not found", Toast.LENGTH_LONG).show();
  136. }
  137. // When Http response code is '500'
  138. else if(statusCode == 500){
  139. Toast.makeText(getApplicationContext(), "Something went wrong at server end", Toast.LENGTH_LONG).show();
  140. }
  141. // When Http response code other than 404, 500
  142. else{
  143. System.out.println(statusCode);
  144. Toast.makeText(getApplicationContext(), "Unexpected Error occcured! [Most common Error: Device might not be connected to Internet or remote server is not up and running] " + statusCode, Toast.LENGTH_LONG).show();
  145. }
  146. }
  147. });
  148. }
  149.  
  150. /**
  151. * Method which navigates from Register Activity to Login Activity
  152. */
  153. public void navigatetoLoginActivity(View view){
  154. Intent loginIntent = new Intent(getApplicationContext(),LoginActivity.class);
  155. // Clears History of Activity
  156. loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  157. startActivity(loginIntent);
  158. }
  159.  
  160. /**
  161. * Set degault values for Edit View controls
  162. */
  163. public void setDefaultValues(){
  164. nameET.setText("");
  165. emailET.setText("");
  166. pwdET.setText("");
  167. }
  168.  
  169. }
  170.  
  171. import java.sql.Connection;
  172. import java.sql.DriverManager;
  173. import java.sql.ResultSet;
  174. import java.sql.SQLException;
  175. import java.sql.Statement;
  176.  
  177. import com.mysql.jdbc.Constants;
  178.  
  179. public class DBConnection {
  180. /**
  181. * Method to create DB Connection
  182. *
  183. * @return
  184. * @throws Exception
  185. */
  186. @SuppressWarnings("finally")
  187. public static Connection createConnection() throws Exception {
  188. Connection con = null;
  189. try {
  190. Class.forName(Constatnts.dbClass);
  191. con = DriverManager.getConnection(Constatnts.dbUrl, Constatnts.dbUser, Constatnts.dbPwd);
  192. } catch (Exception e) {
  193. throw e;
  194. } finally {
  195. return con;
  196. }
  197. }
  198. /**
  199. * Method to check whether uname and pwd combination are correct
  200. *
  201. * @param uname
  202. * @param pwd
  203. * @return
  204. * @throws Exception
  205. */
  206. public static boolean checkLogin(String uname, String pwd) throws Exception {
  207. boolean isUserAvailable = false;
  208. Connection dbConn = null;
  209. try {
  210. try {
  211. dbConn = DBConnection.createConnection();
  212. } catch (Exception e) {
  213. // TODO Auto-generated catch block
  214. e.printStackTrace();
  215. }
  216. Statement stmt = dbConn.createStatement();
  217. String query = "SELECT * FROM user WHERE username = '" + uname
  218. + "' AND password=" + "'" + pwd + "'";
  219. //System.out.println(query);
  220. ResultSet rs = stmt.executeQuery(query);
  221. while (rs.next()) {
  222. //System.out.println(rs.getString(1) + rs.getString(2) + rs.getString(3));
  223. isUserAvailable = true;
  224. }
  225. } catch (SQLException sqle) {
  226. throw sqle;
  227. } catch (Exception e) {
  228. // TODO Auto-generated catch block
  229. if (dbConn != null) {
  230. dbConn.close();
  231. }
  232. throw e;
  233. } finally {
  234. if (dbConn != null) {
  235. dbConn.close();
  236. }
  237. }
  238. return isUserAvailable;
  239. }
  240. /**
  241. * Method to insert uname and pwd in DB
  242. *
  243. * @param name
  244. * @param uname
  245. * @param pwd
  246. * @return
  247. * @throws SQLException
  248. * @throws Exception
  249. */
  250. public static boolean insertUser(String name, String uname, String pwd) throws SQLException, Exception {
  251. System.out.println("entrei no insertuser");
  252. System.out.println(name + " " + uname + " " + pwd);
  253. boolean insertStatus = false;
  254. Connection dbConn = null;
  255. try {
  256. try {
  257. System.out.println("estou no try do insertuser");
  258. dbConn = DBConnection.createConnection();
  259. System.out.println(dbConn);
  260. } catch (Exception e) {
  261. e.printStackTrace();
  262. }
  263. System.out.println("continuando...");
  264. Statement stmt = dbConn.createStatement();
  265. String query = "INSERT into user(name, username, password) values('"+name+ "',"+"'"
  266. + uname + "','" + pwd + "')";
  267. System.out.println(query);
  268. int records = stmt.executeUpdate(query);
  269. System.out.println(records);
  270. //When record is successfully inserted
  271. if (records > 0) {
  272. insertStatus = true;
  273. }
  274. } catch (SQLException sqle) {
  275. //sqle.printStackTrace();
  276. throw sqle;
  277. } catch (Exception e) {
  278. //e.printStackTrace();
  279. // TODO Auto-generated catch block
  280. if (dbConn != null) {
  281. dbConn.close();
  282. }
  283. throw e;
  284. } finally {
  285. if (dbConn != null) {
  286. dbConn.close();
  287. }
  288. }
  289. return insertStatus;
  290. }
  291. }
  292.  
  293. public static String dbUrl = "jdbc:mysql://localhost:3306/"+dbName;
  294.  
  295. public static String dbUser = "root";
  296.  
  297. public static String dbPwd = "";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement