Skip to content

Instantly share code, notes, and snippets.

@rodmoreno
Last active December 17, 2015 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodmoreno/5671531 to your computer and use it in GitHub Desktop.
Save rodmoreno/5671531 to your computer and use it in GitHub Desktop.
Consumir un webservice usando otro thread y la biblioteca ksoap2-android... Una modificación basado en el trabajo de LastDragon (www.lastdragon.net)
package me.rodmoreno.webservice;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class MainActivity extends Activity {
private EditText textoEntrada;
private TextView textoSalida;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textoEntrada = (EditText) findViewById(R.id.entrada);
textoSalida = (TextView) findViewById(R.id.salida);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void clickBoton(View v)
{
new consumirWS().execute(textoEntrada.getText().toString());
}
private class consumirWS extends AsyncTask<String, Void, String>{
final String SOAP_ACTION = "urn:webserv";
final String METHOD = "saludo";
final String NAMESPACE = "urn:webserv";
final String ENDPOINTWS = "http://www.lastdragon.net/misarchivos/webserv/serv.php";
String respuesta = null;
protected String doInBackground(String... args)
{
SoapObject userRequest = new SoapObject(NAMESPACE, METHOD);
userRequest.addProperty("nombre", args[0]);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(userRequest);
try{
HttpTransportSE androidHttpTransport = new HttpTransportSE(ENDPOINTWS);
androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);
respuesta = envelope.getResponse().toString();
}
catch (Exception e){
e.printStackTrace();
}
return respuesta;
}
protected void onPostExecute(String result)
{
textoSalida.setText(result);
super.onPostExecute(String.valueOf(result));
}
}
}
<?php
require_once dirname(__FILE__) . "/lib/nusoap.php";
function saludar($nombre)
{
return "Hola " . $nombre;
}
$servicio = new soap_server();
$servicio->configureWSDL("webserv", "urn:webserv");
$servicio->register("saludar", array("nombre" => "xsd:string"), array("return", => "xsd:string"));
$servicio->service($HTTP_RAW_POST_DATA);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment