我有一个 Web 服务,它需要一个 SOAP 请求来对 String 执行一个简单的方法。现在我在 Android 上编写了应用程序来使用这个 Web 服务,我希望它序列化一些数据并通过 SOAP 将其发送到 Web 服务。
这是一些代码:
public class SendedLocation implements Serializable {
public String MESSAGE;
public SendedLocation() {
}
public SendedLocation(int mId, float mLongitude, float mLatitude) {
MESSAGE = String.valueOf(mId) + ";" + String.valueOf(mLongitude) + ";" + String.valueOf(mLatitude);
}
public Object getProperty(int arg0) {
switch (arg0) {
case 0:
return MESSAGE;
}
return null;
}
public int getPropertyCount() {
return 1;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch (index) {
case 0:
info.type = PropertyInfo.STRING_CLASS;
info.name = "MESSAGE";
break;
default:
break;
}
}
public void setProperty(int index, Object value) {
switch (index) {
case 0:
MESSAGE = value.toString();
break;
default:
break;
}
}
}
public void callWebService(int ID, float Longitude, float Latitude) {
try {
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
URL);
SendedLocation mSended = new SendedLocation(ID, Longitude, Latitude);
PropertyInfo p1 = new PropertyInfo();
p1.setName("mSended");
p1.setValue(mSended);
p1.setType(mSended.getClass());
request.addProperty(p1);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
envelope.addMapping(NAMESPACE, "mSended", mSended.getClass());
MarshalString marshalString = new MarshalString();
marshalString.register(envelope);
// Make the soap call.
androidHttpTransport.call(SOAP_ACTION, envelope);
Object results = (Object) envelope.getResponse();
// to get the data String
// resultData=result.getProperty(0).toString();
String temp = results.toString();
System.out.println(temp);
} catch (Exception aE) {
System.out.println(aE.toString());
}
}
public class MarshalString implements Marshal
{
public Object readInstance(XmlPullParser parser, String namespace, String name,
PropertyInfo expected) throws IOException, XmlPullParserException {
return String.valueOf(parser.nextText());
}
public void register(SoapSerializationEnvelope cm) {
cm.addMapping(cm.xsd, "string", String.class, this);
}
public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
writer.text(obj.toString());
}
}
我在 onCreate() 中调用这个 callWebService() 方法,如下所示:
callWebService(ID , (float)location.getLongitude() , (float)location.getLatitude());
然后,当我运行我的应用程序时,它会从 gps 获得修复,但在发送数据时执行 Web 服务它给了我:
java.lang.RuntimeException: Cannot serialize...
有人可以向我解释我应该添加什么以使其正常工作吗?我真的不知道,在尝试使用元帅类的线索之后......