Passing an object from android to soap service using ksoap2
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 fol开发者_StackOverflow中文版lowing 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.
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
精彩评论