Ver Mensaje Individual
  #1 (permalink)  
Antiguo 14/06/2016, 01:14
sergiuss5
 
Fecha de Ingreso: octubre-2014
Mensajes: 5
Antigüedad: 9 años, 6 meses
Puntos: 0
llamar a web service desde app android

Buenas a todos,

estoy desarrollando para el trabajo una App que permita mandar a un web service un dato y que el web service me de una respuesta.
Lo he hecho en un hilo independiente al hilo principal, pero al dar al boton que lllama al web service la app se cierra.
Estoy perdido y necesito ayuda por favor.
Paso el código que tengo por si alguien puede ayudarme.

Agradezco de antemano cualquier ayuda que me puedan prestar.

Saludos.

package com.example.sergio.wsprueba;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope ;
import org.ksoap2.transport.HttpTransportSE;

public class MainActivity extends Activity {

public static final String NAMESPACE = "urn:lectura";
public static final String METHOD_NAME = "Test";
public static final String URL = "http://www.transzuri.com/ws/lectura/index.php?wsdl";
public static final String SOAP_ACTION = "urn:lectura#Test";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void guid(){
Button btn = (Button) findViewById(R.id.boton);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
miHilo();
}
});
}

final Handler mHandler = new Handler();

protected void miHilo() {
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(ejecutar);
}
};
t.start();
}

final Runnable ejecutar = new Runnable (){

public void run() {
String peticion = "<tns:Test xmlns:tns=\"urn:lectura\"><GUID xsi:type=\"xsd:string\">38B9B858-3B99-43C1-B06D-8750A7028AA4</GUID><BarCode xsi:type=\"xsd:string\">00111166666666601</BarCode></tns:Test>";

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("request", peticion);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.setOutputSoapObject(request);

try {
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
Toast.makeText(getApplicationContext(), "OK!", Toast.LENGTH_SHORT).show();

SoapObject result = (SoapObject) envelope.bodyIn;

if (result != null) {
Toast.makeText(getApplicationContext(), result.getProperty(0).toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "No response", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();
}
}
};


}