Realizando una programa en android me encontré con un problema al conectarme con un WS en dispositivos que utilizaran android versión 4 en adelante, pues resulta que no se puede conectar si lo llamamos directamente en el MainActivity, la solución utilizar AsyncTask
private static final String accionSoap = "urn:example1#example";
private static final String Metodo = "example";
private static final String namespace = "urn:example1"; ////Configuracion del WS
private static final String url = "http://tuurl/servicio?wsdl";
protected void onCreate(Bundle savedInstanceState)
{
new backMethod().execute();/////////////////METODO PARA LLAMAR EL AsyncTask
}
public class backMethod extends AsyncTask<String, Object, Object >
{
private final ProgressDialog dialog = new ProgressDialog(
MainActivity.this);
protected void onPreExecute()
{
dialog.setMessage("HOLA MUNDO..."); ///MENSAJE DE CARGA
dialog.show(); //Mostramos el diálogo antes de comenzar
}
protected void onCancelled(Object result)
{
//super.onCancelled(result);
}
@Override
protected Object doInBackground(String... params)
{
Object resultado = null;
SoapObject request = new SoapObject(namespace1, Metodo1);
SoapSerializationEnvelope sobre = new SoapSerializationEnvelope(SoapEnvelope.VER10);
sobre.dotNet = true;
request.addProperty("data", "1");
sobre.setOutputSoapObject(request);
try
{
HttpTransportSE transporte = new HttpTransportSE(url1);
transporte.call(accionSoap1, sobre);
resultado = (SoapPrimitive) sobre.getResponse();
}
catch (Exception e)
{
e.printStackTrace();
}
return resultado;
}
protected void onPostExecute(Object result)
{
//Here All your UI part is Done
if (result != null)
{
String string = result.toString();//////OBTENEMOS LA RESPUESTA
} else
{
/////////// MENSAJE SI NO HAY RESPUESTA
}
super.onPostExecute(result);
if (this.dialog.isShowing())
{
dialog.dismiss();
}
super.onPostExecute(result);
}
}
Si necesitan ayuda para la solución de su problema y tengo la manera de ayudar con gusto la atenderé.
No hay comentarios:
Publicar un comentario