studio - Crear solicitud SOAP usando KSOAP Android
ksoap2 android studio (3)
Finalmente lo conseguí trabajando con KSOAP. Aquí está el código que utilicé, tal vez ayudará a alguien.
final SoapObject request = new SoapObject(AppConsts.NAMESPACE,
usecaseString);
request.addProperty(addPropertyString, propertyJsonString);
final SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
final HttpTransportSE androidHttpTransport = new HttpTransportSE(
AppConsts.URL);
androidHttpTransport.debug = true;
String soapAction = AppConsts.NAMESPACE + usecaseString;
try {
androidHttpTransport.call(soapAction, envelope);
SoapPrimitive resultSoapPrimitive;
resultSoapPrimitive = (SoapPrimitive) envelope.getResponse();
if (resultSoapPrimitive != null) {
result = resultSoapPrimitive.toString();
if (AppConsts.ENABLE_LOG)
Log.d(AppConsts.GAME_TITLE, "result json : " + result);
} else {
if (AppConsts.ENABLE_LOG)
Log.d(AppConsts.GAME_TITLE, "result json is NULL!!! ");
}
} catch (Exception e) {
e.printStackTrace();
Log.e("static", "Exception in making call to server");
}
Para crear la solicitud anterior, estos tres parámetros debemos pasar al código.
AppConsts.NAMESPACE = "http://playinktennismasters.com"
usecaseString = "authenticateUser"
addPropertyString = "user"
Necesito generar una solicitud de jabón como esta.
SOLICITUD DE JABÓN
POST /TennisMasters/TennisMasters.Listener.asmx HTTP/1.1
Host: playinkstudio.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://playinktennismasters.com/authenticateUser"
<?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>
<authenticateUser xmlns="http://playinktennismasters.com/">
<user>string</user>
</authenticateUser>
</soap:Body>
</soap:Envelope>
Estoy usando KSOAP2 para crear esta solicitud.
private static String SOAP_ACTION = "http://playinktennismasters.com/authenticateUser";
private static String NAMESPACE = "http://playinktennismasters.com/";
private static String METHOD_NAME = "authenticateUser";
private static String URL = "http://playinkstudio.com/TennisMasters/TennisMasters.Listener.asmx";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("user", "A Json String will be here");
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug = true;
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
} catch (Exception e) {
e.printStackTrace();
}
Esta es la solicitud que recibí de la depuración.
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://www.w3.org/2003/05/soap-encoding" xmlns:v="http://www.w3.org/2003/05/soap-envelope">
<v:Header />
<v:Body>
<authenticateUser xmlns="http://playinktennismasters.com/" **id="o0" c:root="1"**>
<user **i:type="d:string"**>{"email":"[email protected]","UserDate":"Feb 22, 2012 7:01:24 PM","GearId":0,"GearValue":0,"Income":0,"Level":0,"MatchResult":0,"MatchType":0,"OfferId":0,"OpponentId":0,"Partners":0,"ExhibitionCount":0,"PowerRuns":0,"PowerServes":0,"PowerShots":0,"Seeds":0,"Energy":0,"Cash":0,"Stamina":0,"Strength":0,"SubLevel":0,"TotalEnergy":0,"TotalStamina":0,"TrainingId":0,"Agility":0,"UserId":0,"Age":0,"ActivityId":0,"gearIsGift":0}</user>
</authenticateUser>
</v:Body>
</v:Envelope>
No sé por qué atributos adicionales como "id" y "c: root" se están agregando en authenticateUser. y atributo adicional en i: type = "d: String". Por favor, puede venir alguien me da un buen ejemplo o tutorial que me puede guiar para crear una solicitud como la anterior, realmente necesito ayuda, gracias.
Utilicé HttpClient simple y Httppost, Simple String para Envelope de solicitud.
String temp = "<?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>"
+ "<authenticateUser xmlns=/"http://playinktennismasters.com//">"
+ "<user>%s</user>" + "</authenticateUser>" + "</soap:Body>"
+ "</soap:Envelope>";
ENVELOPE = String.format(temp, user);
Ahora usa el método que creará los parámetros restantes de la solicitud de publicación y devolverá la cadena de respuesta.
public String CallWebService(String url, String soapAction, String envelope) {
final DefaultHttpClient httpClient = new DefaultHttpClient();
// request parameters
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 15000);
// set parameter
HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);
// POST the envelope
HttpPost httppost = new HttpPost(url);
// add headers
httppost.setHeader("soapaction", soapAction);
httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
String responseString = "Nothingggg";
try {
// the entity holds the request
HttpEntity entity = new StringEntity(envelope);
httppost.setEntity(entity);
// Response handler
ResponseHandler<String> rh = new ResponseHandler<String>() {
// invoked when client receives response
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
// get response entity
HttpEntity entity = response.getEntity();
// read the response as byte array
StringBuffer out = new StringBuffer();
byte[] b = EntityUtils.toByteArray(entity);
// write the response byte array to a string buffer
out.append(new String(b, 0, b.length));
return out.toString();
}
};
responseString = httpClient.execute(httppost, rh);
} catch (Exception e) {
e.printStackTrace();
Log.d("me","Exc : "+ e.toString());
}
// close the connection
httpClient.getConnectionManager().shutdown();
return responseString;
}
para eliminar id ec: atributos de raíz, establezca los adornos en falso:
envelope.setAddAdornments(false);
para eliminar el atributo i: type, para SimpleTypes, establezca los tipos implícitos en true
envelope.implicitTypes = true;
pero cuando se trabaja con ComplexTypes, para eliminar el "i: tipo" necesitará el ksoap 3.0.0 RC1 o superior. Estoy usando 3.0.0 RC2 por ahora, pero voy a actualizar a la versión de lanzamiento estable 3.0.0, cuando esté disponible.