KSOAP 复杂响应解析
KSOAP Complex response parsing
我目前正在为链接到 SOAP WebService 的项目使用 KSOAP2。到目前为止,我已经设法得到一个结果,但现在我想将该结果解析为 POJO。我已经阅读了使用 addMapping(namespace,node,class)
我可以将结果放入该对象,但我不知道代码中的问题出在哪里。
所以我的问题是:How to parse KSOAP response into my custom Object?
wsdl : http://www.webservicex.net/geoipservice.asmx?wsdl
我的XML回复
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetGeoIPResponse xmlns="http://www.webservicex.net/">
<GetGeoIPResult>
<ReturnCode>1</ReturnCode>
<IP>127.0.0.1</IP>
<ReturnCodeDetails>Success</ReturnCodeDetails>
<CountryName>Reserved</CountryName>
<CountryCode>ZZZ</CountryCode>
</GetGeoIPResult>
</GetGeoIPResponse>
</soap:Body>
</soap:Envelope>
MainActivity.java
private static final String SOAP_ACTION = "http://www.webservicex.net/GetGeoIP";
private static final String METHOD_NAME = "GetGeoIP";
private static final String NAMESPACE = "http://www.webservicex.net/";
private static final String URL = "http://www.webservicex.net/geoipservice.asmx?wsdl";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
addProperty("IPAddress","127.0.0.1");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet= true;
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.addMapping(NAMESPACE,"getGeoIPResponse", new GetGeoIPResponse().getClass());
soapEnvelope.addMapping(NAMESPACE,"getGeoIPResult", new GetGeoIPResult().getClass());
soapEnvelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
try {
transport.call(SOAP_ACTION, soapEnvelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
SoapObject resultString = (SoapObject) soapEnvelope.bodyIn;
Log.i(TAG, "Result: " + resultString);
} catch (Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
GetGeoResponse.java
public class GetGeoIPResponse implements KvmSerializable{
GetGeoIPResult getGeoIPResult = new GetGeoIPResult();
@Override
public Object getProperty(int index) {
if( index == 0 ){
return this.getGeoIPResult;
}
return null;
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void setProperty(int index, Object value) {
if( index == 0){
this.getGeoIPResult =(GetGeoIPResult) value;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
if(index == 0){
info.name = "GetGeoIPResult";
info.type = GetGeoIPResult.class;
}
}
}
GetGeoIPResult.java
public class GetGeoIPResult implements KvmSerializable {
private Integer returnCode;
private String IP;
private String returnCodeDetails;
private String countryName;
private String countryCode;
@Override
public Object getProperty(int index) {
switch (index){
case 0: return returnCode;
case 1: return IP;
case 2: return returnCodeDetails;
case 3: return countryName;
case 4: return countryCode;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 5;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
returnCode = Integer.parseInt(value.toString());
break;
case 1:
IP = value.toString();
break;
case 2:
returnCodeDetails = value.toString();
break;
case 3:
countryName = value.toString();
break;
case 4:
countryCode = value.toString();
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "ReturnCode";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "IP";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "ReturnCodeDetails";
info.type = PropertyInfo.STRING_CLASS;
break;
case 3:
info.name = "CountryName";
info.type = PropertyInfo.STRING_CLASS;
break;
case 4:
info.name = "CountryCode";
info.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
}
我实际上发现自己在这里是文件:
请求
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("IPAddress","127.0.0.1");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet= true;
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.addMapping(NAMESPACE,"GetGeoIPResponse", GetGeoIPResponse.class);
soapEnvelope.addMapping(NAMESPACE,"GetGeoIPResult", GetGeoIPResult.class);
soapEnvelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
transport.call(SOAP_ACTION, soapEnvelope);
GetGeoIPResponse resultString = (GetGeoIPResponse) soapEnvelope.bodyIn;
GetGeoIPResult.java
public class GetGeoIPResult implements KvmSerializable {
private Integer returnCode;
private String IP;
private String returnCodeDetails;
private String countryName;
private String countryCode;
@Override
public Object getProperty(int index) {
switch (index){
case 0: return returnCode;
case 1: return IP;
case 2: return returnCodeDetails;
case 3: return countryName;
case 4: return countryCode;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 5;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
returnCode = Integer.parseInt(value.toString());
break;
case 1:
IP = value.toString();
break;
case 2:
returnCodeDetails = value.toString();
break;
case 3:
countryName = value.toString();
break;
case 4:
countryCode = value.toString();
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "ReturnCode";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "IP";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "ReturnCodeDetails";
info.type = PropertyInfo.STRING_CLASS;
break;
case 3:
info.name = "CountryName";
info.type = PropertyInfo.STRING_CLASS;
break;
case 4:
info.name = "CountryCode";
info.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
}
GetGeoIPResponse.java
public class GetGeoIPResponse implements KvmSerializable {
private GetGeoIPResult getGeoIPResult;
@Override
public Object getProperty(int index) {
switch (index){
case 0:
return getGeoIPResult;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
getGeoIPResult = (GetGeoIPResult) value;
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "GetGeoIPResult";
info.type = GetGeoIPResult.class;
break;
}
}
}
我目前正在为链接到 SOAP WebService 的项目使用 KSOAP2。到目前为止,我已经设法得到一个结果,但现在我想将该结果解析为 POJO。我已经阅读了使用 addMapping(namespace,node,class)
我可以将结果放入该对象,但我不知道代码中的问题出在哪里。
所以我的问题是:How to parse KSOAP response into my custom Object?
wsdl : http://www.webservicex.net/geoipservice.asmx?wsdl
我的XML回复
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetGeoIPResponse xmlns="http://www.webservicex.net/">
<GetGeoIPResult>
<ReturnCode>1</ReturnCode>
<IP>127.0.0.1</IP>
<ReturnCodeDetails>Success</ReturnCodeDetails>
<CountryName>Reserved</CountryName>
<CountryCode>ZZZ</CountryCode>
</GetGeoIPResult>
</GetGeoIPResponse>
</soap:Body>
</soap:Envelope>
MainActivity.java
private static final String SOAP_ACTION = "http://www.webservicex.net/GetGeoIP";
private static final String METHOD_NAME = "GetGeoIP";
private static final String NAMESPACE = "http://www.webservicex.net/";
private static final String URL = "http://www.webservicex.net/geoipservice.asmx?wsdl";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
addProperty("IPAddress","127.0.0.1");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet= true;
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.addMapping(NAMESPACE,"getGeoIPResponse", new GetGeoIPResponse().getClass());
soapEnvelope.addMapping(NAMESPACE,"getGeoIPResult", new GetGeoIPResult().getClass());
soapEnvelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
try {
transport.call(SOAP_ACTION, soapEnvelope);
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
SoapObject resultString = (SoapObject) soapEnvelope.bodyIn;
Log.i(TAG, "Result: " + resultString);
} catch (Exception ex) {
Log.e(TAG, "Error: " + ex.getMessage());
}
GetGeoResponse.java
public class GetGeoIPResponse implements KvmSerializable{
GetGeoIPResult getGeoIPResult = new GetGeoIPResult();
@Override
public Object getProperty(int index) {
if( index == 0 ){
return this.getGeoIPResult;
}
return null;
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void setProperty(int index, Object value) {
if( index == 0){
this.getGeoIPResult =(GetGeoIPResult) value;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
if(index == 0){
info.name = "GetGeoIPResult";
info.type = GetGeoIPResult.class;
}
}
}
GetGeoIPResult.java
public class GetGeoIPResult implements KvmSerializable {
private Integer returnCode;
private String IP;
private String returnCodeDetails;
private String countryName;
private String countryCode;
@Override
public Object getProperty(int index) {
switch (index){
case 0: return returnCode;
case 1: return IP;
case 2: return returnCodeDetails;
case 3: return countryName;
case 4: return countryCode;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 5;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
returnCode = Integer.parseInt(value.toString());
break;
case 1:
IP = value.toString();
break;
case 2:
returnCodeDetails = value.toString();
break;
case 3:
countryName = value.toString();
break;
case 4:
countryCode = value.toString();
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "ReturnCode";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "IP";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "ReturnCodeDetails";
info.type = PropertyInfo.STRING_CLASS;
break;
case 3:
info.name = "CountryName";
info.type = PropertyInfo.STRING_CLASS;
break;
case 4:
info.name = "CountryCode";
info.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
}
我实际上发现自己在这里是文件:
请求
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("IPAddress","127.0.0.1");
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet= true;
soapEnvelope.setOutputSoapObject(request);
soapEnvelope.addMapping(NAMESPACE,"GetGeoIPResponse", GetGeoIPResponse.class);
soapEnvelope.addMapping(NAMESPACE,"GetGeoIPResult", GetGeoIPResult.class);
soapEnvelope.implicitTypes = true;
HttpTransportSE transport = new HttpTransportSE(URL);
transport.debug = true;
transport.call(SOAP_ACTION, soapEnvelope);
GetGeoIPResponse resultString = (GetGeoIPResponse) soapEnvelope.bodyIn;
GetGeoIPResult.java
public class GetGeoIPResult implements KvmSerializable {
private Integer returnCode;
private String IP;
private String returnCodeDetails;
private String countryName;
private String countryCode;
@Override
public Object getProperty(int index) {
switch (index){
case 0: return returnCode;
case 1: return IP;
case 2: return returnCodeDetails;
case 3: return countryName;
case 4: return countryCode;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 5;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
returnCode = Integer.parseInt(value.toString());
break;
case 1:
IP = value.toString();
break;
case 2:
returnCodeDetails = value.toString();
break;
case 3:
countryName = value.toString();
break;
case 4:
countryCode = value.toString();
break;
default:
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "ReturnCode";
info.type = PropertyInfo.INTEGER_CLASS;
break;
case 1:
info.name = "IP";
info.type = PropertyInfo.STRING_CLASS;
break;
case 2:
info.name = "ReturnCodeDetails";
info.type = PropertyInfo.STRING_CLASS;
break;
case 3:
info.name = "CountryName";
info.type = PropertyInfo.STRING_CLASS;
break;
case 4:
info.name = "CountryCode";
info.type = PropertyInfo.STRING_CLASS;
break;
default:
break;
}
}
}
GetGeoIPResponse.java
public class GetGeoIPResponse implements KvmSerializable {
private GetGeoIPResult getGeoIPResult;
@Override
public Object getProperty(int index) {
switch (index){
case 0:
return getGeoIPResult;
default: return null;
}
}
@Override
public int getPropertyCount() {
return 1;
}
@Override
public void setProperty(int index, Object value) {
switch (index){
case 0:
getGeoIPResult = (GetGeoIPResult) value;
break;
}
}
@Override
public void getPropertyInfo(int index, Hashtable properties, PropertyInfo info) {
switch (index){
case 0:
info.name = "GetGeoIPResult";
info.type = GetGeoIPResult.class;
break;
}
}
}