简体   繁体   English

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

[英]Passing an object from android to soap service using ksoap2

I have looked at several tutorials using android and ksoap2. 我看过一些使用android和ksoap2的教程。 My service works fine for sending a string as a parameter from the android client to the soap service. 我的服务可以很好地将字符串作为参数从android客户端发送到soap服务。

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). 在服务器端,我有以下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);
}

Very simple. 很简单。 As you can see, the web service operation takes a 'ClientProfile' instance. 如您所见,Web服务操作采用“ ClientProfile”实例。 The 'ClientProfile' class on the server side looks like: 服务器端的“ ClientProfile”类如下所示:

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? 但是,这应该与我的android(客户端)程序包中的“ ClientProfile”类完全相同吗?

Ok. 好。 On the android side, things are a little different. 在android方面,情况有所不同。 I have the following code snippet for invoking the web service: 我有以下用于调用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);

Again, very straightforward. 同样,非常简单。 My ClientProfile class in my android project (client side) looks like: 我的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;
        }
    }
}

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. 但是,在服务器端,当Android仿真器使用kso​​ap2调用Web服务操作时,当我尝试输出作为参数传递的ClientProfile实例时,我将获得“ null”。 It's a real pain. 真是痛苦。

All of the tutorials just show you the class parameter from the android side, not the web service side. 所有这些教程都只是从android而非Web服务方面向您展示了class参数。

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. 来自网络服务的响应可以很好地返回到android客户端,因此这不是问题。

The underlying problem is that the parameter on the web service side is null. 根本问题是Web服务端的参数为null。 Here is the code for my web service operation: 这是我的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) : "";
}

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. 我不知道如何在android模拟器和在localhost上运行的Web服务之间打印出汤消息。

I asked a question about this before but got no reply unfortunately. 我之前曾问过一个问题,但不幸的是没有得到答复。

Thank you. 谢谢。

I found the answer here . 我在这里找到了答案。 For anyone using jax-ws, you must include the annotation for the parameter in the Service Endpoint Interface 对于使用jax-ws的任何人, 必须在服务端点接口中包括该参数的注释。

@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM