Guest User

Untitled

a guest
Dec 22nd, 2017
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.88 KB | None | 0 0
  1. apply plugin: 'com.android.application'
  2.  
  3. android {
  4. compileSdkVersion 23
  5. buildToolsVersion "24.0.2"
  6. defaultConfig {
  7. applicationId "com.foodies.myfoodies"
  8. minSdkVersion 14
  9. targetSdkVersion 23
  10. versionCode 1
  11. versionName "1.0"
  12. testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
  13. }
  14. buildTypes {
  15. release {
  16. minifyEnabled false
  17. proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  18. }
  19. debug {
  20. debuggable true
  21. }
  22. }
  23. }
  24.  
  25. dependencies {
  26. compile fileTree(dir: 'libs', include: ['*.jar'])
  27. androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
  28. exclude group: 'com.android.support', module: 'support-annotations'
  29. })
  30. compile 'com.android.support:appcompat-v7:23.4.0'
  31. compile 'com.android.volley:volley:1.0.0'
  32. testCompile 'junit:junit:4.12'
  33. }
  34.  
  35. public class LoginActivity extends AppCompatActivity {
  36. EditText emailBox, passwordBox;
  37. Button loginButton;
  38. TextView registerLink;
  39. String URL = "http://[ip address]:8081/MyFoodies/LoginRegisterWebService/login";
  40.  
  41. @Override
  42. protected void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.activity_login);
  45. emailBox = (EditText)findViewById(R.id.emailBox);
  46. passwordBox = (EditText)findViewById(R.id.passwordBox);
  47. loginButton = (Button)findViewById(R.id.loginButton);
  48. registerLink = (TextView)findViewById(R.id.registerLink);
  49.  
  50. loginButton.setOnClickListener(new View.OnClickListener() {
  51. @Override
  52. public void onClick(View v) {
  53. StringRequest request = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>(){
  54. @Override
  55. public void onResponse(String rs) {
  56. if(rs.equals("true")){
  57. Toast.makeText(LoginActivity.this, "Login Successful", Toast.LENGTH_LONG).show();
  58. startActivity(new Intent(LoginActivity.this,Home.class));
  59. }
  60. else{
  61. Toast.makeText(LoginActivity.this, "Incorrect Details", Toast.LENGTH_LONG).show();
  62. }
  63. }
  64. },new Response.ErrorListener(){
  65. @Override
  66. public void onErrorResponse(VolleyError volleyError) {
  67. Toast.makeText(LoginActivity.this, "Some error occurred -> "+volleyError, Toast.LENGTH_LONG).show();;
  68. }
  69. }) {
  70. @Override
  71. protected Map<String, String> getParams() throws AuthFailureError {
  72. Map<String, String> parameters = new HashMap<String, String>();
  73. parameters.put("email", emailBox.getText().toString());
  74. parameters.put("password", passwordBox.getText().toString());
  75. return parameters;
  76. }
  77. };
  78.  
  79. request.setRetryPolicy(new DefaultRetryPolicy(
  80. 7000,
  81. DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
  82. DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
  83. RequestQueue rQueue = Volley.newRequestQueue(LoginActivity.this);
  84. int socketTimeout = 10000;//10 seconds - change to what you want
  85. RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
  86. request.setRetryPolicy(policy);
  87. rQueue.add(request);
  88.  
  89. }
  90. });
  91.  
  92. registerLink.setOnClickListener(new View.OnClickListener() {
  93. @Override
  94. public void onClick(View v) {
  95. startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
  96. }
  97. });
  98.  
  99.  
  100. }
  101. }
  102.  
  103. @Path("/LoginRegisterWebService")
  104. public class LoginRegisterWebService {
  105.  
  106. final static String url = "jdbc:mysql://localhost:3307/foodhub";
  107. final static String user = "root";
  108. final static String pass = "root";
  109.  
  110. @POST
  111. @Path("/login")
  112. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  113. @Produces(MediaType.TEXT_HTML)
  114. public String login(@FormParam("email") String email, @FormParam("password") String password){
  115. String result="false";
  116.  
  117. try{
  118. Class.forName("com.mysql.jdbc.Driver");
  119. Connection con = DriverManager.getConnection(url, user, pass);
  120.  
  121. PreparedStatement ps = con.prepareStatement("select * from foodhub.login where email=? and UserPassword=?");
  122. ps.setString(1, email);
  123. ps.setString(2, password);
  124.  
  125. ResultSet rs = ps.executeQuery();
  126.  
  127. if(rs.next()){
  128. result = "true";
  129. }
  130.  
  131. con.close();
  132. }
  133. catch(Exception e){
  134. e.printStackTrace();
  135. }
  136.  
  137. return result;
  138. }
  139.  
  140. @POST
  141. @Path("/register")
  142. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  143. @Produces(MediaType.TEXT_HTML)
  144. public String register(@FormParam("email") String email, @FormParam("password") String password){
  145. String result="false";
  146. int x = 0;
  147.  
  148. try{
  149. Class.forName("com.mysql.jdbc.Driver");
  150. Connection con = DriverManager.getConnection(url, user, pass);
  151.  
  152. PreparedStatement ps = con.prepareStatement("insert into login(email, UserPassword) values(?,?)");
  153. ps.setString(1, email);
  154. ps.setString(2, password);
  155.  
  156. x = ps.executeUpdate();
  157.  
  158. if(x==1){
  159. result = "true";
  160. }
  161.  
  162. con.close();
  163. }
  164. catch(Exception e){
  165. e.printStackTrace();
  166. }
  167.  
  168. return result;
  169. }
  170. }
Add Comment
Please, Sign In to add comment