Foros del Web » Programando para Internet » Android »

saber ip sin que se bloquee el programa

Estas en el tema de saber ip sin que se bloquee el programa en el foro de Android en Foros del Web. He hecho un servicio que en teoría tendría que mirar la ip interna, cada cierto tiempo, y mandarla a una web (incrustado en la dirección ...
  #1 (permalink)  
Antiguo 10/04/2013, 10:15
 
Fecha de Ingreso: octubre-2010
Mensajes: 3
Antigüedad: 13 años, 6 meses
Puntos: 0
Pregunta saber ip sin que se bloquee el programa

He hecho un servicio que en teoría tendría que mirar la ip interna, cada cierto tiempo, y mandarla a una web (incrustado en la dirección como GET).

Tengo este código, y funciona, pero al cabo de un rato, deja de funcionar, aunque el servicio no se para.

He probado barias opciones, pero no consigo que funcione siempre.

No siempre se para al mismo tiempo, a veces puede estar 3 horas funcionando, y otras 1.

Alguien sabe que le pasa, y donde está el fallo. Gracias.

package foo.bar;

import java.io.IOException;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Timer;
import java.util.TimerTask;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class Servicio extends Service{
HttpClient client = new DefaultHttpClient();
String ipactual = getLocalIpAddress();
String ipanterior = "noip";
long numveces = 0;




@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onCreate(){
super.onCreate();



}

@Override
public void onStart(final Intent intent, final int startId){
Timer timer;
timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
mandarurl();
}
}, 0, 60000);

}

public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
return inetAddress.getHostAddress().toString();
}
}
} catch (SocketException ex) {}
return null;
}
/*public static String getLocalIpAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
while(en.hasMoreElements()) {
NetworkInterface intf = en.nextElement();
Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
while(enumIpAddr.hasMoreElements()) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress())
return inetAddress.getHostAddress().toString();
}
}
} catch (SocketException ex) {
Log.e("getLocalIpAddress", ex.toString());
}
return "noip";

}
*/

public void mandarurl(){
ipactual = getLocalIpAddress();
String urlString = "http://web.com/ip.php?ip=" + ipactual + "num." + numveces;
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
URLConnection conn = null;
try {
conn = url.openConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
numveces = numveces + 1;
ipanterior = ipactual;




}

}

Etiquetas: ip, java
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:52.