【问题标题】:Cannot access java web service in Android无法在 Android 中访问 java web 服务
【发布时间】:2016-06-06 07:13:58
【问题描述】:

我尝试使用 java 创建 android 登录 Web 服务。我正在使用 Axis2。

我正在使用 Eclipse EE 开发 Web 服务,并使用 Eclipse Adt Bundle 开发 Android 应用程序。我可以访问“http://localhost:8081/Login/services/Login?wsdl”页面。当 android 应用程序运行并单击登录按钮时,我在屏幕上看不到任何消息(在 web 服务 status="success" 或 status="login fail" 内发出)。

我没有解决这个问题。任何帮助将不胜感激。

网络服务:

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;

    }

}

安卓:

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){

        }
       }

}

【问题讨论】:

  • android 的 localhost 不是你电脑的 localhost
  • 我尝试使用 10.0.2.2:8081 访问。但不能解决问题。
  • (但在您真正查看错误之前您不会知道)
  • 您似乎对“本地主机”有一些误解,或者在主线程上不允许进行网络操作,或者期望在其他地方完成的操作的结果立即可用或成为您的东西可以做一个阻塞等待。

标签: java android web-services android-ksoap2


【解决方案1】:

您正在压缩 android 代码中的异常:

    catch(Exception e){

    }

这很有可能会丢弃可以告诉您根本问题是什么的证据。无论哪种方式,像这样压缩Exception 都是非常糟糕的做法。

【讨论】:

  • 是的,但这更多的是作为一般实践和调试建议的评论,而不是对这个问题的实际答案。
  • @ChrisStratton - 我不同意。解决这个问题是解决他的问题的第一步,因此是他问题的实际答案。如果运气好的话,他将能够自己解决剩下的问题。唯一合法的做法是将其关闭为“离题 - 证据不足”。
  • 虽然这是一个很好的调试建议,但如果花时间在android 和正在使用的库。
  • @ChrisStratton - 我不同意。我们可以>>guess
  • 如果我们花时间了解 Android 上的网络规则和所使用的 soap 库,我们不必猜测。
【解决方案2】:

在 android 代码中,您尝试连接到 localhost,但它应该是运行 java 服务的主机名。在 android 设备中,您显然没有在端口 8081 上提供任何服务。 这是一个很常见的错误。通常您在同一台机器上开发服务器端和 android 应用程序,因此当在 localhost 上运行服务时,您认为相同的服务应该在 android 应用程序中可用。但是,在模拟器中,localhost 是 android 设备的地址。开发和测试此类应用程序的最简单方法是使用主机的 IP 地址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-09
    • 1970-01-01
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多