0

I try to create android login web service with java. I am using Axis2.

I am developing web service with Eclipse EE and android application with Eclipse Adt Bundle. I can access "http://localhost:8081/Login/services/Login?wsdl" page. When android application ran and clicked login button, i am not seeing any message (issued inside web services status="success" or status="login fail")on the screen.

I didn't solve this problem.Any help will be appreciated.

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/places", "root",
                    "");
            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;

    }

}

Android:

package 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://localhost:8081/Login/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){

        }
       }

}
B.Bus
  • 11
  • 1
  • 2
  • 8
  • android's localhost is not your computer's localhost – njzk2 Jun 05 '16 at 23:21
  • I tried to access with 10.0.2.2:8081. But cannot solve problem. – B.Bus Jun 06 '16 at 00:28
  • there is a good chance it is simply https://stackoverflow.com/questions/6343166/how-to-fix-android-os-networkonmainthreadexception – njzk2 Jun 06 '16 at 01:00
  • (but you won't know until you actually take a look at your error) – njzk2 Jun 06 '16 at 01:01
  • You appear to have some mix of misunderstanding "localhost" along with either impermissibly doing a network operation on the main thread, or else expecting the result of one done elsewhere to be immediately available or to be something you can do a blocking wait on. – Chris Stratton Jun 06 '16 at 03:06

2 Answers2

0

You are squashing exceptions in the android code:

    catch(Exception e){

    }

There's a good chance that that is discarding the evidence that would tell you what the underlying problem is. Either way, squashing Exception like that is very bad practice.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • True, but that's more of a comment as a general practice and debug suggestion, than an actual answer to this question. – Chris Stratton Jun 05 '16 at 23:51
  • 1
    @ChrisStratton - I disagree. Fixing this is the first step in solving his problem, and therefore an actual answer to his question. With a bit of luck, he will be able to work out the rest for himself. The only other legit thing to do is to close it as "off-topic - insufficient evidence". – Stephen C Jun 06 '16 at 02:50
  • While this is a good debugging suggestion, the *actual* problems can be recognized from the code which has already been provided, if one takes the time to understand networking in the context of android and the library being used. – Chris Stratton Jun 06 '16 at 03:07
  • @ChrisStratton - I disagree. We can >>guess<< the likely causes of problems, but without evidence of the stacktrace, we can't exclude other possible causes. – Stephen C Jun 06 '16 at 03:14
  • If we take time to understand the rules of networking on Android and the soap library being used, **we don't have to guess.** – Chris Stratton Jun 06 '16 at 03:16
  • How do you know it isn't an NPE? (E.g. here `result.setText(...)`) You ARE guessing. Or more correctly, you are incorrectly excluding possible diagnoses. – Stephen C Jun 06 '16 at 03:17
  • The part of the program within the silent try{} block that could potentially not be deterministic by inspection would be one that tried to interact with an external network. In Android code running on the main thread, as the posted code is, is not permitted to do networking operations. – Chris Stratton Jun 06 '16 at 03:24
0

In android code you trying to connect to localhost, but it should a host name where the java service running. In the android device you obviously don't have any service available on port 8081. This is quite a common mistake. Usually you develop server side and android application on the same machine so when running a service on localhost you believe that the same service should be available in the android application. However, in the emulator, the localhost is the address of the android device. The easiest way to develop and test such applications is to use ip address of the host machine.

Mikhail Chibel
  • 1,865
  • 1
  • 22
  • 34