0

I am trying to get data from SQL server to my android app. First I have connected my app with a web service built in C# like this:

public class DataHelper
{
    public static Cliente[] ListadoClientes()
    {
        DataSet dst = new DataSet();
        string instrumentos = "";
        /*try
        {*/
        var connectionString = 
            ConfigurationManager.ConnectionStrings["TestAndroid"].ConnectionString;
        //create string connections
        SqlConnection con = new SqlConnection(connectionString);
        //open connections
        con.Open();
        /* if (con != null)
        {*/
        //query to database
        SqlCommand cmd = new SqlCommand(
            "select clrut,Clnombre,Clcodigo from parametros..CLIENTE", con);
        SqlDataReader dr = cmd.ExecuteReader();
        List<Cliente> lista = new List<Cliente>();
        while (dr.Read())
        {
            lista.Add(new Cliente(dr.GetDecimal(0),
                dr.GetString(1),
                dr.GetDecimal(2)));
        }
        //dr.Close();
        con.Close();
        return lista.ToArray();
    }
}

That code is server-side and I'm capturing the data on android.

//Tarea Asíncrona para llamar al WS de consulta en segundo plano
private class TareaWSConsulta extends AsyncTask<String,Integer,Boolean> {
    private Cliente[] listaClientes;
    protected Boolean doInBackground(String... params) {
        boolean resul = true;
        final String NAMESPACE = "http://xxxx.net";
        final String URL="http://iplocaliis/ServicioClientes.asmx";
        final String METHOD_NAME = "metodoCliente";
        final String SOAP_ACTION ="http://xxxxxx/ListadoClientes";
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        SoapSerializationEnvelope envelope =
            new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(request);
        HttpTransportSE transporte = new HttpTransportSE(URL);
        try {
            transporte.call(SOAP_ACTION, envelope);
            SoapObject resSoap =(SoapObject)envelope.getResponse();
            listaClientes = new Cliente[resSoap.getPropertyCount()];
            for (int i = 0; i < listaClientes.length; i++) {
                SoapObject ic = (SoapObject)resSoap.getProperty(i);
                Cliente cli = new Cliente();
                cli.id = Integer.parseInt(ic.getProperty(0).toString());
                cli.nombre = ic.getProperty(1).toString();
                cli.telefono = Integer.parseInt(ic.getProperty(2).toString());
                listaClientes[i] = cli;
            }
        } 
        catch (Exception e) 
        {
            resul = false;
        } 
        return resul;
    }
    protected void onPostExecute(Boolean result) {
        if (result) {
            //Rellenamos la lista con los nombres de los clientes
            final String[] datos = new String[listaClientes.length];
            for(int i=0; i<listaClientes.length; i++)
                datos[i] = listaClientes[i].nombre;
            ArrayAdapter<String> adaptador =
                new ArrayAdapter<String>(MainActivity.this,
                android.R.layout.simple_list_item_1, datos);
            lstClientes.setAdapter(adaptador);
        }
        else {
            txtResultado.setText("Error!");
        }
    }
}

It works good with Wi-Fi but when I change my connection to 3G it doesn't work. The web services are published in a local IIS.

2 Answers 2

1

That's a network issue, you can't access to your local web services from a public IP address unless you configure your router.

Sign up to request clarification or add additional context in comments.

1 Comment

but, how can i acces to my web service from my app out of my network? maybe publishing the web service in a hosting with support in sql sever?
0

There's no problem with your code, it's problem of networking.

1. Why it works with Wifi connected ?

When your server and phone both connected with a same WLAN, they both in a local network, so your app can access the server.

2. Why not 3G network ?

Once the phone connected with 3G network, then phone and server are not in a same network. In addition, your server IP is not a public IP, so your app can not access to your server.

3. How to make server accessible

  • Apply a public IP for your server.
  • Buy cloud server, and deploy your webservice on the server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.