假设你有webmethod这样定义返回Category对象。
[WebMethod]
public Category GetCategoryById(Category C);
说Category类有三个属性,如类别编号,名称和Description.So与KSOAP使用它,你必须实现KvmSerializable
。
package CommonObjects;
import java.util.Hashtable;
import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;
public class Category implements KvmSerializable
{
public int CategoryId;
public String Name;
public String Description;
public Category(){}
public Category(int categoryId, String name, String description) {
CategoryId = categoryId;
Name = name;
Description = description;
}
public Object getProperty(int arg0) {
switch(arg0)
{
case 0:
return CategoryId;
case 1:
return Name;
case 2:
return Description;
}
return null;
}
public int getPropertyCount() {
return 3;
}
public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
switch(index)
{
case 0:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "CategoryId";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Name";
break;
case 2:
info.type = PropertyInfo.STRING_CLASS;
info.name = "Description";
break;
default:break;
}
}
public void setProperty(int index, Object value) {
switch(index)
{
case 0:
CategoryId = Integer.parseInt(value.toString());
break;
case 1:
Name = value.toString();
break;
case 2:
Description = value.toString();
break;
default:
break;
}
}
}
现在你必须映射属性,读取它们并写入它们。
,使Web服务调用是:
public void WebServiceCallExample()
{
String NAMESPACE = "http://vladozver.org/";
String METHOD_NAME = "GetCategoryById";
String SOAP_ACTION = "http://vladozver.org/GetCategoryById";
String URL = "http://192.168.1.3/VipEvents/Services/CategoryServices.asmx";
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
/*
* Create Category with Id to be passed as an argument
*
* */
Category C = new Category();
C.CategoryId = 1;
/*
* Set the category to be the argument of the web service method
*
* */
PropertyInfo pi = new PropertyInfo();
pi.setName("C");
pi.setValue(C);
pi.setType(C.getClass());
Request.addProperty(pi);
/*
* Set the web service envelope
*
* */
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(URL);
/*
* Call the web service and retrieve result ... how luvly <3
*
* */
try
{
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
C.CategoryId = Integer.parseInt(response.getProperty(0).toString());
C.Name = response.getProperty(1).toString();
C.Description = (String) response.getProperty(2).toString();
TextView tv = (TextView)findViewById(R.id.TextView01);
tv.setText("CategoryId: " +C.CategoryId + " Name: " + C.Name + " Description " + C.Description);
}
catch(Exception e)
{
e.printStackTrace();
}
}
为了使用此代码,请确保您替换该方法与自己的前三个变量。注意:SOAP_ACTION实际上可以计算为NAMESPACE + METHOD_NAME
在关于属性信息的一行中,我演示了如何将一个Category类复杂对象实际传递给KSOAP。
PropertyInfo pi = new PropertyInfo();
pi.setName("Category");
pi.setValue(C);
pi.setType(C.getClass());
Request.addProperty(pi);
关于返回类型,如果你的Web方法返回一个复杂的对象(如我们的),你需要告诉KSOAP如何处理响应。即用下面的代码完成:
envelope.addMapping(NAMESPACE, "Category",new Category().getClass());
的这部分代码是响应的实际检索:从这个blogpost
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject)envelope.getResponse();
C.CategoryId = Integer.parseInt(response.getProperty(0).toString());
C.Name = response.getProperty(1).toString();
C.Description = (String) response.getProperty(2).toString();
参考。
你的代码在哪里?邮编至少 –
是的Ksoap lib是好的。但是,你得到什么错误/什么问题?你见过这个http://programmerguru.com/android-tutorial/how-to-call-java-web-service-in -android/ –
什么是你必须发送请求的格式(结构)....? –