Ver Mensaje Individual
  #3 (permalink)  
Antiguo 29/05/2013, 10:00
Avatar de RodMoreno
RodMoreno
 
Fecha de Ingreso: abril-2013
Ubicación: México
Mensajes: 4
Antigüedad: 11 años
Puntos: 0
Respuesta: Cómo consumir WebService en otro hilo?

Muchas gracias providez, me aclaraste algunas dudas y ya se encuentra funcionando... :D

Tuviste mucha razón ademas de que me di cuenta de que instrumentaba la clase del AsyncTask fuera de la clase del Activity y debe instrumentarse dentro.

Código Java:
Ver original
  1. package name.rodmoreno.webservice4;
  2.  
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.view.Menu;
  7. import android.view.View;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. import org.ksoap2.SoapEnvelope;
  12. import org.ksoap2.serialization.SoapObject;
  13. import org.ksoap2.serialization.SoapSerializationEnvelope;
  14. import org.ksoap2.transport.HttpTransportSE;
  15.  
  16. public class MainActivity extends Activity {
  17.  
  18.     private EditText textoEntrada;
  19.     private TextView textoSalida;
  20.  
  21.     @Override
  22.     protected void onCreate(Bundle savedInstanceState) {
  23.         super.onCreate(savedInstanceState);
  24.         setContentView(R.layout.activity_main);
  25.  
  26.         textoEntrada = (EditText) findViewById(R.id.entrada);
  27.         textoSalida = (TextView) findViewById(R.id.salida);
  28.     }
  29.  
  30.  
  31.     @Override
  32.     public boolean onCreateOptionsMenu(Menu menu) {
  33.         // Inflate the menu; this adds items to the action bar if it is present.
  34.         getMenuInflater().inflate(R.menu.main, menu);
  35.         return true;
  36.     }
  37.  
  38.     public void clickBoton(View v)
  39.     {
  40.         new consumirWS().execute(textoEntrada.getText().toString());
  41.     }
  42.  
  43.     private class consumirWS extends AsyncTask<String, Void, String>{
  44.         final String SOAP_ACTION = "urn:webserv";
  45.         final String METHOD = "saludo";
  46.         final String NAMESPACE = "urn:webserv";
  47.         final String ENDPOINTWS = "http://192.168.1.65/servicio.php";
  48.         String respuesta = null;
  49.  
  50.  
  51.         protected String doInBackground(String... args)
  52.         {
  53.             SoapObject userRequest = new SoapObject(NAMESPACE, METHOD);
  54.             userRequest.addProperty("nombre", args[0]);
  55.  
  56.             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
  57.             envelope.setOutputSoapObject(userRequest);
  58.  
  59.             try{
  60.                 HttpTransportSE androidHttpTransport = new HttpTransportSE(ENDPOINTWS);
  61.                 androidHttpTransport.debug = true;
  62.                 androidHttpTransport.call(SOAP_ACTION, envelope);
  63.  
  64.                 respuesta = envelope.getResponse().toString();
  65.             }
  66.             catch (Exception e){
  67.                 e.printStackTrace();
  68.             }
  69.  
  70.             return respuesta;
  71.         }
  72.  
  73.         protected void onPostExecute(String result)
  74.         {
  75.             textoSalida.setText(result);
  76.  
  77.             super.onPostExecute(String.valueOf(result));
  78.         }
  79.     }
  80.    
  81. }