Android App that consumes a remote web service

1

I am new to this, I will try to be specific. I already have the web service published on the iis of a windows server 2012, I managed entity framework to access a SQLServer DB and through a webmethod insert data in a table and run the WS locally in the browser if you insert the data, up there everything is fine but, when you want to do it with android it just does not do anything. I already have the ksoap2 library installed, I have seen endless tutorials and no doubt that this is what I still have to do. I enclose my android code, I use threads for the version of android I use, otherwise it thunders, it is a method that is activated by pressing a button. thanks in advance.

    public void Cita(View v) {

    Thread nt = new Thread(){

        EditText nombre = (EditText)findViewById(R.id.txtNombre);
        EditText apellido = (EditText)findViewById(R.id.txtApellido);
        EditText fecha = (EditText)findViewById(R.id.txtFecha);
        EditText empleado = (EditText)findViewById(R.id.txtEmpleado);
        EditText servicio = (EditText)findViewById(R.id.txtServicio);
        String res;
        @Override
        public void run(){
            String NAMESPACE = "http://tempuri.org/";
            String URL = "http://192.168.1.253/ServidorWeb/SWS.asmx";
            String METHOD_NAME = "AgregarCita";
            String SOAP_ACTION = "http://tempuri.org/AgregarCita";

            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

            request.addProperty("nombre",nombre.getText().toString());
            request.addProperty("apellido",apellido.getText().toString());
            request.addProperty("fecha",fecha.getText().toString());
            request.addProperty("idEmpleado",Integer.parseInt(empleado.getText().toString()));
            request.addProperty("idServicio",Integer.parseInt(servicio.getText().toString()));


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

            HttpTransportSE transporte = new HttpTransportSE(URL);

            try{
                transporte.call(SOAP_ACTION, envelope);
                SoapObject resultado = (SoapObject) envelope.getResponse();
                res = resultado.toString();
                res += "Agregado";
            }catch(IOException e){
                e.printStackTrace();
                res += "Error";
            }catch (XmlPullParserException e){
                e.printStackTrace();
                res += "Error2";
            }

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(InsertaCita.this, res, Toast.LENGTH_SHORT).show();
                }
            });
        }
    };
    nt.start();
}

Important fact that I ignored, regardless of giving Internet permission in the manifest, I must add this:

  ConnectivityManager cm =

(ConnectivityManager) context.getSystemService (Context.CONNECTIVITY_SERVICE);

NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork.isConnectedOrConnecting();
    
asked by Iván Gonzalez 05.12.2017 в 17:05
source

1 answer

0

In this case, you have directly tried the "AddCita" method, accessing the url link , you comment that you get as a result:

 <string>Insertado</string>

Therefore we can determine that the Web Service is working correctly.

Now let's see, checking your code I can point out the problem, envelope.getResponse() get a SoapObject therefore you should not cast% a SoapPrimitive :

 SoapPrimitive resultado = (SoapPrimitive)envelope.getResponse();

You must change to:

 SoapObject resultado = (SoapObject) envelope.getResponse();

To obtain a SoapObject with the result of the query to the Web Service.

There is no other comment but remember to give internet permissions to your application, set it in AndroidManifest.xml :

<uses-permission android:name="android.permission.INTERNET" />
    
answered by 05.12.2017 в 18:43