0

我的计划是构建一个可以在我的 Android 应用程序和任何 Web 框架(如 CodeIgniter 或 YII)上读取的 Web 服务,但我希望它首先在 Android 上实现。我已经尝试弄清楚我需要什么,但我只能获得一些来源,因为我没有这方面的任何经验。

我试图在 REST 和 SOAP 之间学习,

REST:我首先选择 REST,然后尝试本教程,但是我在 Eclipse 和 Netbeans IDE 上构建的 Web 服务存在问题,这就是我在我的 tomcat 服务器上运行我的 Web 服务时发生的情况,Tomcat 未能启动并在控制台上显示为这样。(实际上,我已经在那个教程页面上发布了这个问题,但是作者还没有回答我的问题)。

Jun 21, 2012 7:49:11 AM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jre7\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre7/bin/client;C:/Program Files/Java/jre7/bin;C:/Program Files/Java/jre7/lib/i386;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\WIDCOMM\Bluetooth Software\;C:\Program Files\Java\jdk1.6.0_16\bin;C:\Program Files\Java\jdk1.7.0_01\bin;C:\opencv\build\common\tbb\ia32\vc10\;C:\opencv\build\x86\vc10\bin\;C:\Server\apache-tomcat-6.0.26\bin;;D:\Progima\Eclipse JEE Indigo\eclipse;;.
Jun 21, 2012 7:49:11 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:Luna’ did not find a matching property.
Jun 21, 2012 7:49:12 AM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Jun 21, 2012 7:49:12 AM org.apache.coyote.AbstractProtocolHandler init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Jun 21, 2012 7:49:12 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 612 ms
Jun 21, 2012 7:49:12 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jun 21, 2012 7:49:12 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.12
java.lang.IllegalAccessError: class com.sun.media.sound.AbstractPlayer cannot access its superclass com.sun.media.sound.AbstractMidiDevice
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1591)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at org.apache.catalina.startup.ContextConfig.checkHandlesTypes(ContextConfig.java:1956)
at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:1919)
at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1806)
at org.apache.catalina.startup.ContextConfig.processAnnotationsUrl(ContextConfig.java:1765)
at org.apache.catalina.startup.ContextConfig.processAnnotations(ContextConfig.java:1751)
at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1255)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:882)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:317)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:89)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5081)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:774)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1033)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:291)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:727)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
at org.apache.catalina.startup.Catalina.start(Catalina.java:620)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:303)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:431)

但是当我尝试在 Eclipse >> General >> Switch Location 上右键单击 Tomcat 服务器,然后再次运行时,Tomcat 是可以启动的。tomcat 已启动,但是当我运行 localhost:8080[slash]RestWebServiceDemo[slash]rest[slash]person (基于本教程)时,它仍然出现错误 404。我不知道会发生什么。

SOAP:然后,我想尝试 REST 之外的其他解决方案。我找到了这个教程,我已经尝试过了,我在 Eclipse 和 Netbeans 双方都构建了 Web 服务。在 Eclipse 中,当我尝试启动服务器时,我上面描述的同样的事情再次发生。在Netbeans上,webservice运行正常,可以正常启动,可以运行wsdl localhost:9090[slash]Ladodi[slash]Hello?wsdl。但是,当我尝试从我的 Android 获取它时,数​​据没有出现在我的 TextView 上。

我的 Netbeans 上的代码如下所示。我构建了两个操作,带参数和不带参数,因为在教程上,操作是不带参数的,当然我后面的操作需要参数。

package love.care;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 *
 * @author Setyadi
 */
@WebService()
public class Hello {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "Hai")
    public String Hai(@WebParam(name = "nama")
    String nama) {
        //TODO write your implementation code here:
        return "Hai";
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "Kerupuk")
    public String Kerupuk() {
        //TODO write your implementation code here:
        return "Cocote";
    }

}

这是我的 Eclipse 上的 android 代码,用于获取“Kerupuk”操作(不带参数)。

package in.figure.on.mobile;

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.widget.TextView;

public class WebServiceActivity extends Activity{

    private static final String NAMESPACE = "http://care.love/";
    private static String URL = "http://localhost:9090/Ladodi/Hello?wsdl"; 
    private static final String METHOD_NAME = "Kerupuk";
    private static final String SOAP_ACTION =  "http://care.love/Kerupuk";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE ht = new HttpTransportSE(URL);
        try {
         ht.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            TextView helloTV = (TextView) findViewById(R.id.textHello);
            helloTV.setText("Asaa :"+response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

这是我的 Eclipse 上用于获取“Hai”操作(带参数)的 android 代码,我想将“AdityaSetyadi”作为参数传递。

package in.figure.on.mobile;

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.widget.TextView;

public class WebServiceActivity extends Activity{

    private static final String NAMESPACE = "http://care.love/";
    private static String URL = "http://localhost:9090/Ladodi/Hello?wsdl"; 
    private static final String METHOD_NAME = "Hai";
    private static final String SOAP_ACTION =  "http://care.love/Hai";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);  

          PropertyInfo propInfo = new PropertyInfo();
          propInfo.setName("nama");
          propInfo.setValue("AdityaSetyadi");
          propInfo.setType(PropertyInfo.STRING_CLASS);
          request.addProperty(propInfo);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);

        HttpTransportSE ht = new HttpTransportSE(URL);
        try {
         ht.call(SOAP_ACTION, envelope);
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

            TextView helloTV = (TextView) findViewById(R.id.textHello);
            helloTV.setText("Asaa :"+response.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

两者的区别在于PropertyInfo。我尝试使用 addProperty 传递参数,我尝试使用本教程中的代码,但是当尝试像这样编写代码时。但是 addProperty 已被弃用,所以我尝试在上面编写代码。

        PropertyInfo propInfo=new PropertyInfo();
        propInfo.name="arg0";
        propInfo.type=PropertyInfo.STRING_CLASS;

        request.addProperty(propInfo, "John Smith"); 

但是数据仍然没有出现在我的TextView上,我不知道该怎么办。有人可以帮忙吗?或者再给我一个教程。在此先感谢,最好的问候,@AdityaSetyadi

:'(

4

2 回答 2

0

由于您的 Web 服务是 JAX-WS 而不是 .NET,因此我建议您替换envelope.dotNet = true;envelope.dotNet = false;

于 2012-09-28T09:58:21.130 回答
0

你为什么不试试这个SoapObject respone = (SoapObject) envelope.bodyIn; 而不是这条线

SoapPrimitive response = (SoapPrimitive) envelope.getResponse();

如果你想添加参数,还有另一种方法

request.addProperty("parameter",value);

代替PropertyInfo

和其他重要的事情可能是你做了这一切

1)将使用互联网权限添加到项目的清单文件中

2)请检查NAMESPACESOAP_ACTION是否根据您的正确书写Webservicewebservice您可以通过在浏览器中打开来验证它。有NAMESPACESOAP_ACTION两者都提到了。

如果您有任何疑问,请随时问我。

于 2012-06-27T04:45:35.173 回答