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.
这是一个网络问题,除非您配置路由器,否则您无法从公共 IP 地址访问本地 Web 服务。
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.