android - Android 中的 Web 服务 SOAP

标签 android web-services ksoap2

我开始在 Android 中使用网络服务,并且我遵循了本教程:Create Web Service in Java Using Apache Axis2 and Eclipse创建和使用网络服务。本教程解释了 Java 中的客户端应用程序,但通过其他一些教程,我能够像这样在 Android 中使用 WS:

package com.android.webservices;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class AddNumbersWSActivity extends Activity {
/** Called when the activity is first created. */

private String METHOD_NAME = "addTwoNumbers"; // our webservice method name
private String NAMESPACE = "http://sencide.com"; // Here package name in webservice with reverse order.
private String SOAP_ACTION = "http://sencide.com/addTwoNumbers"; // NAMESPACE + method name
private static final String URL = "http://192.168.1.214:8080/axis2/services/FirstWebService?wsdl"; // you must use ipaddress here, don’t use Hostname or localhost

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button addButton = (Button) findViewById(R.id.button1);

    addButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            try
            {
                SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
                EditText num1 = (EditText) findViewById(R.id.editText1);
                EditText num2 = (EditText) findViewById(R.id.editText2);
                request.addProperty("firstNumber", Integer.valueOf(num1.getText().toString()));
                request.addProperty("secondNumber", Integer.valueOf(num2.getText().toString()));
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
//                  envelope.dotNet = true;
                    envelope.setOutputSoapObject(request);
                    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
                    androidHttpTransport.call(SOAP_ACTION,envelope);
                    Object result = envelope.getResponse();
                    System.out.println("Result : " + result.toString());
                    ((TextView) findViewById (R.id.textView4)).setText(result.toString());
                } catch (Exception E) {
                    E.printStackTrace();
                }
            }
        });

    }
}

但现在我对在 Android 中使用 WS 有一些疑问:

  1. 如我所见here ,kSOAP2 库已弃用...我应该继续使用这个库还是需要其他工具?如果有,是哪一个?
  2. 是否可以仅使用服务的 WSDL 了解NAMESPACEMETHOD_NAMESOAP_ACTION
  3. 我可以使用 WSDL(例如用于 Java 应用程序的 WSDL2Java 工具)自动生成一些代码吗?
  4. addProperty(String name, Object value) 方法中,我可以使用任意 name 还是应该遵循规则?

提前致谢!

最佳答案

Apache Axis 适用于 Java,不适用于 Android 平台。 Ksoap2 库是这个的替代品,也是在 Android 中使用 web 服务最常用的工具。对于您的问题:

1 - 今天 [15.5.2012] 我只能在 4 天前在 here 中看到最后一次提交

2 - 是的,这是可能的。命名空间描述如下:

<wsdl:definitions targetNamespace="http://tempuri.org/">

方法名称描述如下:

<s:element name="YourMethodName">

而SOAP action一般由NAMESPACE + METHOD_NAME组成;

3 - 据我所知,目前还没有写得很好的项目。

4 - 它应该遵循必须符合 WSDL 的属性名称和区分大小写的规则

关于android - Android 中的 Web 服务 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10598734/

相关文章:

android - 找不到 ksoap2 HttpTransport 类

android - 无法解析资源@style.AppTheme.AppBarOverlay

java - wsimport 上的 WSDL 错误

android - 适用于 Android 应用程序的 RESTful Web 服务

java - 发送 HTTP 请求抛出 JAVA 时出现 405 错误

android - 从 Android 应用程序调用 Web 服务

android - 如何获取对 WebView 系统 bundle 字体的字体引用? (格鲁吉亚,Times New Roman..)

android - Image Picker Intent - 在线存储照片的空路径

Android在ListView中实现聊天气泡

java - Android应用程序无法解析主机