使用kso​​ap2将对象从android传递到soap服务

发布于 2024-11-26 05:27:38 字数 3729 浏览 0 评论 0原文

我看过几个使用 android 和 ksoap2 的教程。我的服务工作正常,可以将字符串作为参数从 android 客户端发送到soap 服务。

现在,如果我想传递一个对象,而不是传递一个字符串。但是,它对我不起作用。

在服务器端,我有以下 Web 服务操作(我使用的是 jax-ws)。

package soap.service.sei;

import javax.jws.WebService;

import soap.service.data.ClientProfile;

@WebService(name = "ImageSei", targetNamespace = "http://image.webservice")
public interface ImageSei {
    public String getImage(ClientProfile c);
}

很简单。如您所见,Web 服务操作采用“ClientProfile”实例。 服务器端上的“ClientProfile”类看起来像:

package soap.service.data;

public class ClientProfile {
    public int ram;
    public String cpu;
    public String bandwidth;
}

同样,非常简单。但是,这应该与我的 android(客户端)包中的“ClientProfile”类完全相同吗?

好的。在 Android 方面,情况有些不同。我有以下用于调用 Web 服务的代码片段:

    ClientProfile c = new ClientProfile();
    c.ram = 256;
    c.cpu = "22.445";
    c.bandwidth = "712.2";
    prop.setName("c");
    prop.setValue(c);
    prop.setType(c.getClass());
    request.addProperty(prop);

同样,非常简单。我的 android 项目(客户端) 中的 ClientProfile 类如下所示:

package soap.service.data;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class ClientProfile implements KvmSerializable {
    public int ram;
    public String cpu;
    public String bandwidth;

    public ClientProfile() {}

    public ClientProfile(int $ram, String $cpu, String $bandwidth) {
        ram = $ram;
        cpu = $cpu;
        bandwidth = $bandwidth;
    }

    @Override
    public Object getProperty(int arg0) {
        switch(arg0) {
            case 0 :
                return ram;
            case 1 :
                return cpu;
            case 2 :
                return bandwidth;
        }
        return null;
    }

    @Override
    public int getPropertyCount() {
        return 3;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index) {
            case 0 : 
                info.type = PropertyInfo.INTEGER_CLASS;
                info.name = "ram";
                break;
            case 1 :
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "cpu";
                break;
            case 2 : 
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "bandwidth";
                break;
            default : 
                break;
        }
    }

    @Override
    public void setProperty(int index, Object value) {
        switch(index) {
            case 0 :
                ram = Integer.parseInt(value.toString());
                break;
            case 1 :
                cpu = value.toString();
                break;
            case 2 :
                bandwidth = value.toString();
                break;
            default :
                break;
        }
    }
}

但是,在服务器端,当 android 模拟器使用 ksoap2 调用 Web 服务操作时,我当我尝试输出作为参数传递的 ClientProfile 实例时,得到“null”。这真的很痛苦。

所有教程都只是向您展示 Android 端的类参数,而不是 Web 服务端。

我假设可能存在某种序列化问题?无论如何,我没有例外。 Web 服务的响应可以很好地返回到 Android 客户端,因此这不是问题。

根本问题是Web服务端的参数为空。这是我的网络服务操作的代码:

@Override
public String getImage(ClientProfile c) {
    System.out.println(c); // ***** THIS IS NULL *****
    byte[] imageBytes = null;
    try {
           //...
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return (imageBytes != null) ? Base64.encodeBase64String(imageBytes) : "";
}

我真的很感谢这里的一些帮助,因为我完全陷入困境。我不知道如何打印我的 android 模拟器和本地主机上运行的 Web 服务之间的 soup 消息。

我之前问过这个问题,但不幸的是没有得到答复。

谢谢。

I have looked at several tutorials using android and ksoap2. My service works fine for sending a string as a parameter from the android client to the soap service.

Now, instead of passing a string, if I want to pass an object. However, it won't work for me.

On the server side, I have the following web service operation (I'm using jax-ws).

package soap.service.sei;

import javax.jws.WebService;

import soap.service.data.ClientProfile;

@WebService(name = "ImageSei", targetNamespace = "http://image.webservice")
public interface ImageSei {
    public String getImage(ClientProfile c);
}

Very simple. As you can see, the web service operation takes a 'ClientProfile' instance. The 'ClientProfile' class on the server side looks like:

package soap.service.data;

public class ClientProfile {
    public int ram;
    public String cpu;
    public String bandwidth;
}

Again, very simple. However, should this be the exact same as the 'ClientProfile' class in my android (the client) package?

Ok. On the android side, things are a little different. I have the following code snippet for invoking the web service:

    ClientProfile c = new ClientProfile();
    c.ram = 256;
    c.cpu = "22.445";
    c.bandwidth = "712.2";
    prop.setName("c");
    prop.setValue(c);
    prop.setType(c.getClass());
    request.addProperty(prop);

Again, very straightforward. My ClientProfile class in my android project (client side) looks like:

package soap.service.data;

import java.util.Hashtable;

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class ClientProfile implements KvmSerializable {
    public int ram;
    public String cpu;
    public String bandwidth;

    public ClientProfile() {}

    public ClientProfile(int $ram, String $cpu, String $bandwidth) {
        ram = $ram;
        cpu = $cpu;
        bandwidth = $bandwidth;
    }

    @Override
    public Object getProperty(int arg0) {
        switch(arg0) {
            case 0 :
                return ram;
            case 1 :
                return cpu;
            case 2 :
                return bandwidth;
        }
        return null;
    }

    @Override
    public int getPropertyCount() {
        return 3;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
        switch(index) {
            case 0 : 
                info.type = PropertyInfo.INTEGER_CLASS;
                info.name = "ram";
                break;
            case 1 :
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "cpu";
                break;
            case 2 : 
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "bandwidth";
                break;
            default : 
                break;
        }
    }

    @Override
    public void setProperty(int index, Object value) {
        switch(index) {
            case 0 :
                ram = Integer.parseInt(value.toString());
                break;
            case 1 :
                cpu = value.toString();
                break;
            case 2 :
                bandwidth = value.toString();
                break;
            default :
                break;
        }
    }
}

However, on the server side, when the web service operation is invoked by the android emulator using ksoap2, I get 'null' when I try to output the ClientProfile instance that is passed as a parameter. It's a real pain.

All of the tutorials just show you the class parameter from the android side, not the web service side.

I'm assuming there is some sort of serialization issue maybe? Regardless, I get no exceptions. The response from the web service comes back to the android client fine, so this is not a problem.

The underlying problem is that the parameter on the web service side is null. Here is the code for my web service operation:

@Override
public String getImage(ClientProfile c) {
    System.out.println(c); // ***** THIS IS NULL *****
    byte[] imageBytes = null;
    try {
           //...
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    return (imageBytes != null) ? Base64.encodeBase64String(imageBytes) : "";
}

I would really appreciate some help here as I'm completely stuck. I don't know how to print out the soup messages between my android emulator and the web service running on localhost.

I asked a question about this before but got no reply unfortunately.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月苍老的讽刺 2024-12-03 05:27:38

我在此处找到了答案。对于使用 jax-ws 的任何人,您必须在服务端点接口中包含参数的注释。

@WebService(name = "ImageSei", targetNamespace = "http://image.webservice")
public interface ImageSei {
    public String getImage(@WebParam(name = "c")ClientProfile c);
}

一旦您执行此操作,它就应该可以工作。输出如下:

soap.service.data.ClientProfile@349471

I found the answer here. For anyone using jax-ws, you must include the annotation for the parameter in the Service Endpoint Interface

@WebService(name = "ImageSei", targetNamespace = "http://image.webservice")
public interface ImageSei {
    public String getImage(@WebParam(name = "c")ClientProfile c);
}

Once you do this, it should work. Output is as follows:

soap.service.data.ClientProfile@349471
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文