Android:如何将对象类发送到web服务
我正在尝试使用ksoap2 android向我的ASP.NET Web服务发送一个对象类 SOAP请求:Android:如何将对象类发送到web服务,android,asp.net,web-services,ksoap2,android-ksoap2,Android,Asp.net,Web Services,Ksoap2,Android Ksoap2,我正在尝试使用ksoap2 android向我的ASP.NET Web服务发送一个对象类 SOAP请求: <?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<sendTest xmlns="http://testing.com/">
<test>
<Id>int</Id>
<Name>string</Name>
<Age>int</Age>
</test>
</sendTest>
</soap:Body>
</soap:Envelope>
public class eTest
{
int id;
string name;
int age;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
public class Test implements KvmSerializable {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public Object getProperty(int index) {
switch (index) {
case 0:
return getId();
case 1:
return getName();
case 2:
return getAge();
}
return null;
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.name = "Id";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "Name";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "Age";
info.type = PropertyInfo.INTEGER_CLASS;
break;
default:
break;
}
}
@Override
public int getPropertyCount() {
return 3;
}
@Override
public void setProperty(int index, Object value) {
switch (index) {
case 0:
setId(Integer.parseInt(value.toString()));
break;
case 1:
setName(value.toString());
break;
case 2:
setAge(Integer.parseInt(value.toString()));
break;
default:
break;
}
}
}
public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
String SOAP_ACTION = "http://testing.com/sendTest";
String METHOD_NAME = "sendTest";
String NAMESPACE = "http://testing.com/";
String URL = "http://testing.com/WebService.asmx";
Test test = new Test();
test.setId(1);
test.setName("Charly");
test.setAge(26);
PropertyInfo property = new PropertyInfo();
property.setName("test");
property.setType(Test.class);
property.setValue(test);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(property);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE Transport = new HttpTransportSE(URL);
try {
Transport.call(SOAP_ACTION, envelope);
return true;
} catch (Exception e) {
Log.e("ERROR", "KSOAP2", e);
}
return false;
}
}
Java类:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<sendTest xmlns="http://testing.com/">
<test>
<Id>int</Id>
<Name>string</Name>
<Age>int</Age>
</test>
</sendTest>
</soap:Body>
</soap:Envelope>
public class eTest
{
int id;
string name;
int age;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
public class Test implements KvmSerializable {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public Object getProperty(int index) {
switch (index) {
case 0:
return getId();
case 1:
return getName();
case 2:
return getAge();
}
return null;
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.name = "Id";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "Name";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "Age";
info.type = PropertyInfo.INTEGER_CLASS;
break;
default:
break;
}
}
@Override
public int getPropertyCount() {
return 3;
}
@Override
public void setProperty(int index, Object value) {
switch (index) {
case 0:
setId(Integer.parseInt(value.toString()));
break;
case 1:
setName(value.toString());
break;
case 2:
setAge(Integer.parseInt(value.toString()));
break;
default:
break;
}
}
}
public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
String SOAP_ACTION = "http://testing.com/sendTest";
String METHOD_NAME = "sendTest";
String NAMESPACE = "http://testing.com/";
String URL = "http://testing.com/WebService.asmx";
Test test = new Test();
test.setId(1);
test.setName("Charly");
test.setAge(26);
PropertyInfo property = new PropertyInfo();
property.setName("test");
property.setType(Test.class);
property.setValue(test);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(property);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE Transport = new HttpTransportSE(URL);
try {
Transport.call(SOAP_ACTION, envelope);
return true;
} catch (Exception e) {
Log.e("ERROR", "KSOAP2", e);
}
return false;
}
}
异步任务:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<sendTest xmlns="http://testing.com/">
<test>
<Id>int</Id>
<Name>string</Name>
<Age>int</Age>
</test>
</sendTest>
</soap:Body>
</soap:Envelope>
public class eTest
{
int id;
string name;
int age;
public int Id
{
get { return id; }
set { id = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
public class Test implements KvmSerializable {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public Object getProperty(int index) {
switch (index) {
case 0:
return getId();
case 1:
return getName();
case 2:
return getAge();
}
return null;
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index) {
case 0:
info.name = "Id";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "Name";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "Age";
info.type = PropertyInfo.INTEGER_CLASS;
break;
default:
break;
}
}
@Override
public int getPropertyCount() {
return 3;
}
@Override
public void setProperty(int index, Object value) {
switch (index) {
case 0:
setId(Integer.parseInt(value.toString()));
break;
case 1:
setName(value.toString());
break;
case 2:
setAge(Integer.parseInt(value.toString()));
break;
default:
break;
}
}
}
public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
String SOAP_ACTION = "http://testing.com/sendTest";
String METHOD_NAME = "sendTest";
String NAMESPACE = "http://testing.com/";
String URL = "http://testing.com/WebService.asmx";
Test test = new Test();
test.setId(1);
test.setName("Charly");
test.setAge(26);
PropertyInfo property = new PropertyInfo();
property.setName("test");
property.setType(Test.class);
property.setValue(test);
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(property);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE Transport = new HttpTransportSE(URL);
try {
Transport.call(SOAP_ACTION, envelope);
return true;
} catch (Exception e) {
Log.e("ERROR", "KSOAP2", e);
}
return false;
}
}
公共类MyAsyncTask扩展了AsyncTask{
@凌驾
受保护的布尔doInBackground(Void…params){
字符串SOAP_ACTION=”http://testing.com/sendTest";
字符串方法\u NAME=“sendTest”;
字符串命名空间=”http://testing.com/";
字符串URL=”http://testing.com/WebService.asmx";
测试=新测试();
test.setId(1);
测试集名称(“Charly”);
试验设置(26);
PropertyInfo属性=新的PropertyInfo();
设置名称(“测试”);
setType(Test.class);
属性设置值(测试);
SoapObject请求=新的SoapObject(名称空间、方法名称);
请求。添加属性(属性);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
HttpTransportSE传输=新的HttpTransportSE(URL);
试一试{
传输呼叫(SOAP_动作、信封);
返回true;
}捕获(例外e){
Log.e(“错误”,“KSOAP2”,e);
}
返回false;
}
}
错误在哪里?我在Logcat中没有任何例外sendTest
方法是Sql查询的插入,但当我执行AsyncTask时,什么都没有发生。问题解决了
envelope.addMapping(NAMESPACE, "eTest", Test.class);
如果我有这样一个int长字符串的请求