使用ksoap2将对象从android传递到soap服务
我已经看过几个使用android和ksoap2的教程。我的服务可以将字符串作为参数从android客户端发送到soap服务 现在,如果我想传递一个对象,而不是传递一个字符串。然而,这对我不起作用 在服务器端,我有以下web服务操作(我使用的是jax ws) 很简单。如您所见,web服务操作采用“ClientProfile”实例。服务器端的“ClientProfile”类类似于:使用ksoap2将对象从android传递到soap服务,android,ksoap2,Android,Ksoap2,我已经看过几个使用android和ksoap2的教程。我的服务可以将字符串作为参数从android客户端发送到soap服务 现在,如果我想传递一个对象,而不是传递一个字符串。然而,这对我不起作用 在服务器端,我有以下web服务操作(我使用的是jax ws) 很简单。如您所见,web服务操作采用“ClientProfile”实例。服务器端的“ClientProfile”类类似于: package soap.service.data; public class ClientProfile {
package soap.service.data;
public class ClientProfile {
public int ram;
public String cpu;
public String bandwidth;
}
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(客户端)包中的“ClientProfile”类完全相同
嗯。在安卓方面,情况有点不同。我有以下用于调用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;
public class ClientProfile {
public int ram;
public String cpu;
public String bandwidth;
}
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 emulator使用ksoap2调用web服务操作时,当我尝试输出作为参数传递的ClientProfile实例时,会得到“null”。真的很痛苦
所有教程都只是从android端而不是web服务端显示class参数
我假设可能存在某种序列化问题?不管怎样,我没有例外。来自web服务的响应很好地返回到android客户端,因此这不是问题
根本的问题是web服务端的参数为null。以下是我的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模拟器和运行在localhost上的web服务之间打印soup消息
我以前问过一个问题,但很遗憾没有得到答复
谢谢。我找到了答案。对于任何使用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