
llamar web service desde App
Publicado por Sergio (5 intervenciones) el 13/06/2016 12:15:30
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.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.example.sergio.wsprueba;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
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);
}
final Handler miHandler = new Handler();
protected void miHilo() {
Thread t = new Thread() {
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
miHandler.post(ejecutar);
}
};
t.start();
}
class ejecutar extends Thread {
@Override
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();
}
}
}
void mensaje(View view) {
miHilo() hilo = new miHilo();
hilo.start();
}
}
Valora esta pregunta


0