(In latest versions of Android you cannot access the web in same way mentioned here. if you are planning implement web access for version 3.0 or higher than that follow one of below methods
1.Async Task
http://codeoncloud.blogspot.com/2013/07/android-web-service-access-using-async.html
2.Handlers
http://codeoncloud.blogspot.com/2013/06/android-java-soap-web-service-access.html )
Quick demo :
The complete project has three main components
1. A MySQL database which holds user name and password.
2. A java web service deployed on Tomcat server.
3.Android application to access the database through the java web service to verify the user.
1. Databse
First we have to create a database and table to store user information. To create the database I used MySQL command line client. (If you like you can use phpmyadmin it is easier)In order to create the database, a table and to populate the database run following queries.
a. Create the database
CREATE DATABSE androidlogin;b. Select the database
USE androidlogin;c. Create a table
CREATE TABLE user( username VARCHAR(20) NOT NULL, password VARCHAR(20) NOT NULL);d.Populate the database
INSERT INTO user(username,password) VALUES('admin','123');
2. Java webservice
Create the java web service in Eclipse. Follow the steps mentioned here. Additionally you have to import JDBC connector to the web service project. Here is the content for java web service.
package com.userlogin.ws; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class Login { public String authentication(String userName,String password){ String retrievedUserName = ""; String retrievedPassword = ""; String status = ""; try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/androidlogin","root","chathura"); PreparedStatement statement = con.prepareStatement("SELECT * FROM user WHERE username = '"+userName+"'"); ResultSet result = statement.executeQuery(); while(result.next()){ retrievedUserName = result.getString("username"); retrievedPassword = result.getString("password"); } if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)){ status = "Success!"; } else{ status = "Login fail!!!"; } } catch(Exception e){ e.printStackTrace(); } return status; } }
> For more details read my first post.
> "root" and "chathura" in line 17 are user and the password of the database. You need to change those according to your settings.
3. Android application.
a. Code for main activitypackage com.androidlogin.ws; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.PropertyInfo; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class AndroidLoginExampleActivity extends Activity { private final String NAMESPACE = "http://ws.userlogin.com"; private final String URL = "http://111.223.128.10:8085/AndroidLogin/services/Login?wsdl"; private final String SOAP_ACTION = "http://ws.userlogin.com/authentication"; private final String METHOD_NAME = "authentication"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button login = (Button) findViewById(R.id.btn_login); login.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { loginAction(); } }); } private void loginAction(){ SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); EditText userName = (EditText) findViewById(R.id.tf_userName); String user_Name = userName.getText().toString(); EditText userPassword = (EditText) findViewById(R.id.tf_password); String user_Password = userPassword.getText().toString(); //Pass value for userName variable of the web service PropertyInfo unameProp =new PropertyInfo(); unameProp.setName("userName");//Define the variable name in the web service method unameProp.setValue(user_Name);//set value for userName variable unameProp.setType(String.class);//Define the type of the variable request.addProperty(unameProp);//Pass properties to the variable //Pass value for Password variable of the web service PropertyInfo passwordProp =new PropertyInfo(); passwordProp.setName("password"); passwordProp.setValue(user_Password); passwordProp.setType(String.class); request.addProperty(passwordProp); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); envelope.setOutputSoapObject(request); HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); try{ androidHttpTransport.call(SOAP_ACTION, envelope); SoapPrimitive response = (SoapPrimitive)envelope.getResponse(); TextView result = (TextView) findViewById(R.id.tv_status); result.setText(response.toString()); } catch(Exception e){ } } }
> You need to import ksoap2 library for the Android project.
> You cannot access the URL in above code because the web service is deployed in Tomcat server installed in my computer not in a cloud server therefore you have to replace that with an URL for your own web service.
> For more details check these posts.
1. Post 1
2. Post 2
b. Content of main.xml
c. You need to add internet permission to the project
Manifest.xml
You can download Android project here
(After downloading the project first remove imported ksoap2 library from the project and re import ksoap2 from your hard disk )
Please post your ideas :)