当我尝试发出请求时,我在Android上遇到了ksoap2库的问题。
在我创建请求对象之后,当我进行http调用时,一些对象被复制并被赋值为null。这当然给我带来了问题,因为服务响应对象不应该为空。
使用fiddler我发现了问题所在。 我的编码的相关部分看起来像这样:
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.implicitTypes = false;
soapEnvelope.dotNet = false;
SoapObject soapReq = new SoapObject(NAMESPACE, METHOD_NAME);
soapEnvelope.addMapping(NAMESPACE,"request",new client_request().getClass());
MarshalFloat marshalFloat = new MarshalFloat();
marshalFloat.register(soapEnvelope);
soapReq.addProperty("request",request);
soapEnvelope.setOutputSoapObject(soapReq);
HttpTransportSE httpTransport = new HttpTransportSE(url,timeOut);
soapEnvelope.headerOut= new Element[]{get_header()};
try{
if (headers!=null){
httpTransport.call(SOAP_ACTION, soapEnvelope,headers);
}else{
httpTransport.call(SOAP_ACTION, soapEnvelope); }
}
我的电话发布的相关xml标签是:
<owner i:type="d:anyType">
<person_type i:type="d:string">sometext</person_type>
</owner>
<owner i:null="true" />
<duration i:type="d:int">6</duration>
<duration i:null="true" />
<duration i:null="true" />
如何摆脱这些标签:
<owner i:null="true" />
<duration i:null="true" />
<duration i:null="true" />
我正在使用Ksoap2 3.4.0版本并使用了wsdl客户端生成器。 如果有人可以帮助我,我将非常感激,我真的没有想法。如果需要,我可以添加更多信息。
更新: 所以这是我的客户请求类和请求的内容:
public class client_request implements KvmSerializable {
public autentificare_sas autentificare_sas;
public String societate;
public vehicul vehicul;
public owner owner;
//public owner utilizator;
public String clasa_bm_anterioara;
public reduceri reduceri;
public majorari majorari;
public String data_inceput;
public int durata;
/* public boolean generare_oferta;
public boolean generare_ofertaSpecified;*/
public client_request(){}
public client_request(SoapObject soapObject)
{
if (soapObject == null)
return;
if (soapObject.hasProperty("autentificare_sas"))
{
SoapObject j = (SoapObject)soapObject.getProperty("autentificare_sas");
autentificare_sas = new autentificare_sas (j);
}
if (soapObject.hasProperty("societate"))
{
Object obj = soapObject.getProperty("societate");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
societate = j.toString();
}else if (obj!= null && obj instanceof String){
societate = (String) obj;
}
}
if (soapObject.hasProperty("vehicul"))
{
SoapObject j = (SoapObject)soapObject.getProperty("vehicul");
vehicul = new vehicul (j);
}
if (soapObject.hasProperty("owner"))
{
SoapObject j = (SoapObject)soapObject.getProperty("owner");
owner = new owner (j);
}
/* if (soapObject.hasProperty("utilizator"))
{
SoapObject j = (SoapObject)soapObject.getProperty("utilizator");
utilizator = new owner (j);
}*/
if (soapObject.hasProperty("clasa_bm_anterioara"))
{
Object obj = soapObject.getProperty("clasa_bm_anterioara");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
clasa_bm_anterioara = j.toString();
}else if (obj!= null && obj instanceof String){
clasa_bm_anterioara = (String) obj;
}
}
if (soapObject.hasProperty("reduceri"))
{
SoapObject j = (SoapObject)soapObject.getProperty("reduceri");
reduceri = new reduceri (j);
}
if (soapObject.hasProperty("majorari"))
{
SoapObject j = (SoapObject)soapObject.getProperty("majorari");
majorari = new majorari (j);
}
if (soapObject.hasProperty("data_inceput"))
{
Object obj = soapObject.getProperty("data_inceput");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
data_inceput = j.toString();
}else if (obj!= null && obj instanceof String){
data_inceput = (String) obj;
}
}
if (soapObject.hasProperty("durata"))
{
Object obj = soapObject.getProperty("durata");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
durata = Integer.parseInt(j.toString());
}else if (obj!= null && obj instanceof Number){
durata = (Integer) obj;
}
}
/* if (soapObject.hasProperty("generare_oferta"))
{
Object obj = soapObject.getProperty("generare_oferta");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
generare_oferta = Boolean.parseBoolean(j.toString());
}else if (obj!= null && obj instanceof Boolean){
generare_oferta = (Boolean) obj;
}
}
if (soapObject.hasProperty("generare_ofertaSpecified"))
{
Object obj = soapObject.getProperty("generare_ofertaSpecified");
if (obj != null && obj.getClass().equals(SoapPrimitive.class)){
SoapPrimitive j =(SoapPrimitive) obj;
generare_ofertaSpecified = Boolean.parseBoolean(j.toString());
}else if (obj!= null && obj instanceof Boolean){
generare_ofertaSpecified = (Boolean) obj;
}
}*/
}
@Override
public Object getProperty(int arg0) {
switch(arg0){
case 0:
return autentificare_sas;
case 1:
return societate;
case 2:
return vehicul;
case 3:
return owner;
/* case 4:
return utilizator;*/
case 5:
return clasa_bm_anterioara;
case 6:
return reduceri;
case 7:
return majorari;
case 8:
return data_inceput;
case 9:
return durata;
/* case 10:
return generare_oferta;
case 11:
return generare_ofertaSpecified;*/
}
return null;
}
@Override
public int getPropertyCount() {
return 12;
}
@Override
public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
switch(index){
case 0:
info.type = autentificare_sas.class;
info.name = "autentificare_sas";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "societate";
break;
case 2:
info.type = vehicul.class;
info.name = "vehicul";
break;
case 3:
info.type = PropertyInfo.OBJECT_CLASS;
info.name = "owner";
break;
/* case 4:
info.type = owner.class;
info.name = "utilizator";
break;*/
case 5:
info.type = PropertyInfo.STRING_CLASS;
info.name = "clasa_bm_anterioara";
break;
case 6:
info.type = reduceri.class;
info.name = "reduceri";
break;
case 7:
info.type = majorari.class;
info.name = "majorari";
break;
case 8:
info.type = PropertyInfo.STRING_CLASS;
info.name = "data_inceput";
break;
case 9:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "durata";
break;
/* case 10:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "generare_oferta";
break;
case 11:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "generare_ofertaSpecified";
break;*/
}
}
@Override
public void setProperty(int arg0, Object arg1) {
}
@Override
public String getInnerText() {
return null;
}
@Override
public void setInnerText(String s) {
}
}
public void buildClientRequest(){
client_request myRequest = new client_request();
myRequest.durata = 6;
myRequest.data_inceput = "2014-12-01";
myRequest.clasa_bm_anterioara = "B0";
myRequest.vehicul = buildVehicul();
myRequest.owner = buildOwner();
myRequest.societate = "XXXXXX";
client_response myCLientResp = myInstance.get_client(myRequest);
}
public vehicul buildVehicul(){
vehicul myVehicul = new vehicul();
myVehicul.numar_inmatriculare = "XX-99-YYY";
myVehicul.tip_inmatriculare = WS_Enums.tipuri_inmatriculare.inmatriculat;
myVehicul.serie_sasiu = "WSDF123123154";
myVehicul.categorie = 1;
myVehicul.subcategorie = "Autoturism de teren";
myVehicul.marca = "XXXXX";
myVehicul.model = "YYYYYYYY";
myVehicul.an_fabricatie = 2005;
myVehicul.capacitate_cilindrica = 1461;
myVehicul.putere = 63;
myVehicul.masa_maxima = 1065;
myVehicul.numar_locuri = 5;
myVehicul.combustibil = WS_Enums.tipuri_combustibili.benzina;
myVehicul.tip_utilizare = WS_Enums.tipuri_utilizare.personal;
myVehicul.carte_identitate = "H123123";
return myVehicul;
}
public owner buildOwner(){
owner myOwner = new owner();
myOwner.tip_persoana = WS_Enums.tip_persoana.fizica;
myOwner.cod_unic = "12345678912345";
myOwner.nume = "XXXXX";
myOwner.prenume = "YYYYY";
myOwner.societate = null;
myOwner.adresa = buildAdresa();
myOwner.data_permis_conducere = "2012-02-29";
myOwner.bugetar = true;
myOwner.numar_daune = 3;
myOwner.societate_de_leasing = false;
myOwner.domeniu_activitate = 3;
myOwner.categorie_pj = WS_Enums.categorie_pj.regiiautonome;
return myOwner;
}
public adresa buildAdresa(){
adresa myAdresa = new adresa();
myAdresa.localitate_siruta = 999999;
myAdresa.judet = "CJ";
myAdresa.strada = "Principala";
return myAdresa;
}
我只添加了构建请求的方法,我希望它是这样的。 在我打电话之后:
client_response myCLientResp = myInstance.get_client(myRequest);
我到达了之前展示过的部分,信封的创建和调用正在进行中。
我认为问题可能是所有者的类型:
info.type = PropertyInfo.OBJECT_CLASS;
info.name = "owner";
这就是我放置PropertyInfo.OBJECT_CLASS的原因,但这并没有解决任何问题。
提前致谢。
答案 0 :(得分:1)
我解决了这个问题。
问题在于,在我的请求中,我注释掉了两个元素,而不是那些使用当然最后一个实例创建的空标记。
@Override
public void getPropertyInfo(int index, @SuppressWarnings("rawtypes") Hashtable arg1, PropertyInfo info) {
switch(index){
case 0:
info.type = autentificare_sas.class;
info.name = "autentificare_sas";
break;
case 1:
info.type = PropertyInfo.STRING_CLASS;
info.name = "societate";
break;
case 2:
info.type = vehicul.class;
info.name = "vehicul";
break;
case 3:
info.type = PropertyInfo.OBJECT_CLASS;
info.name = "owner";
break;
/* case 4:
info.type = owner.class;
info.name = "utilizator";
break;*/
case 5:
info.type = PropertyInfo.STRING_CLASS;
info.name = "clasa_bm_anterioara";
break;
case 6:
info.type = reduceri.class;
info.name = "reduceri";
break;
case 7:
info.type = majorari.class;
info.name = "majorari";
break;
case 8:
info.type = PropertyInfo.STRING_CLASS;
info.name = "data_inceput";
break;
case 9:
info.type = PropertyInfo.INTEGER_CLASS;
info.name = "durata";
break;
/* case 10:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "generare_oferta";
break;
case 11:
info.type = PropertyInfo.BOOLEAN_CLASS;
info.name = "generare_ofertaSpecified";
break;*/
}
}
因此,对于那些遇到同样问题的人,请注意您从请求中注释的内容: - )。
干杯