Advertisement
Guest User

Untitled

a guest
Jun 5th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.14 KB | None | 0 0
  1. package com.userlogin.ws;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7.  
  8. public class Login {
  9. public String authentication(String userName, String password) {
  10.  
  11. String retrievedUserName = "";
  12. String retrievedPassword = "";
  13. String status = "";
  14. try {
  15.  
  16. Class.forName("com.mysql.jdbc.Driver");
  17. Connection con = DriverManager.getConnection(
  18. "jdbc:mysql://localhost:3306/places", "root",
  19. "");
  20. PreparedStatement statement = con
  21. .prepareStatement("SELECT * FROM user WHERE username = '"
  22. + userName + "'");
  23. ResultSet result = statement.executeQuery();
  24.  
  25. while (result.next()) {
  26. retrievedUserName = result.getString("username");
  27. retrievedPassword = result.getString("password");
  28. }
  29.  
  30. if (retrievedUserName.equals(userName)
  31. && retrievedPassword.equals(password)) {
  32. status = "Success!";
  33. }
  34.  
  35. else {
  36. status = "Login fail!!!";
  37. }
  38.  
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. return status;
  43.  
  44. }
  45.  
  46. }
  47.  
  48. package com.androidlogin.ws;
  49.  
  50. import org.ksoap2.SoapEnvelope;
  51. import org.ksoap2.serialization.PropertyInfo;
  52. import org.ksoap2.serialization.SoapObject;
  53. import org.ksoap2.serialization.SoapPrimitive;
  54. import org.ksoap2.serialization.SoapSerializationEnvelope;
  55. import org.ksoap2.transport.HttpTransportSE;
  56. import android.app.Activity;
  57. import android.os.Bundle;
  58. import android.view.View;
  59. import android.widget.Button;
  60. import android.widget.EditText;
  61. import android.widget.TextView;
  62.  
  63. public class AndroidLoginExampleActivity extends Activity {
  64. private final String NAMESPACE = "http://ws.userlogin.com";
  65. private final String URL = "http://localhost:8081/Login/services/Login?wsdl";
  66. private final String SOAP_ACTION = "http://ws.userlogin.com/authentication";
  67. private final String METHOD_NAME = "authentication";
  68. /** Called when the activity is first created. */
  69. @Override
  70. public void onCreate(Bundle savedInstanceState) {
  71. super.onCreate(savedInstanceState);
  72. setContentView(R.layout.main);
  73. Button login = (Button) findViewById(R.id.btn_login);
  74. login.setOnClickListener(new View.OnClickListener() {
  75.  
  76. public void onClick(View arg0) {
  77. loginAction();
  78.  
  79. }
  80. });
  81. }
  82.  
  83. private void loginAction(){
  84. SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  85.  
  86. EditText userName = (EditText) findViewById(R.id.tf_userName);
  87. String user_Name = userName.getText().toString();
  88. EditText userPassword = (EditText) findViewById(R.id.tf_password);
  89. String user_Password = userPassword.getText().toString();
  90.  
  91. //Pass value for userName variable of the web service
  92. PropertyInfo unameProp =new PropertyInfo();
  93. unameProp.setName("userName");//Define the variable name in the web service method
  94. unameProp.setValue(user_Name);//set value for userName variable
  95. unameProp.setType(String.class);//Define the type of the variable
  96. request.addProperty(unameProp);//Pass properties to the variable
  97.  
  98. //Pass value for Password variable of the web service
  99. PropertyInfo passwordProp =new PropertyInfo();
  100. passwordProp.setName("password");
  101. passwordProp.setValue(user_Password);
  102. passwordProp.setType(String.class);
  103. request.addProperty(passwordProp);
  104.  
  105. SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  106. envelope.setOutputSoapObject(request);
  107. HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  108.  
  109. try{
  110. androidHttpTransport.call(SOAP_ACTION, envelope);
  111. SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
  112.  
  113. TextView result = (TextView) findViewById(R.id.tv_status);
  114. result.setText(response.toString());
  115.  
  116. }
  117. catch(Exception e){
  118.  
  119. }
  120. }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement