1

Is it possible to get the public IP address of the device using some Java code in Android?

Can I use curl with php to remote server to know the public IP? But I don't know how to use curl in Java.

I need the code for user's country based on the public IP address.

I Only need the code for public ip of the user later see the country

Gatiko06
  • 299
  • 1
  • 3
  • 10
  • 2
    The device doesn't even know. You'll need a web-service to report it back to you. – 323go Jan 29 '14 at 18:43
  • @323go what!? it does not say its behind a NAT, it can be a direct connection, so the device will know. Even if its behind a NAT, by UPnP it can inform the IP to the device. – Diego C Nascimento Jan 29 '14 at 18:45
  • @DiegoCNascimento, the linked question is not a duplicate. The poster asked about the **remote** address, not the local address, which is easy to get. Would you kindly point out which part of the UPnP spec provides discovery of the remote address? I suppose you might be able to get to it through SSDP against the router/gateway, but it would be significantly less involved to go through an external service, as it also confirms connectivity. – 323go Jan 29 '14 at 18:51
  • @DiegoCNascimento, please no sent me negative feedback this not are duplicate, I need remote address not localhost NOT the local Ip of router DHCP – Gatiko06 Jan 29 '14 at 18:55
  • So I don't understand the question. The IP address of the device in the Internet is not a remote address its the **local** public address. Is this you are talking or you are talking about other device IP address!? – Diego C Nascimento Jan 29 '14 at 18:59
  • @323go no I don't have searched the documentations, I have experience that confirms that, in my system and in other systems too. I don't have time to search that for others, but this is not remote IP address if I understand the question and as I have explained in the last comment. Using an external service should be the last option as the service could go down. – Diego C Nascimento Jan 29 '14 at 19:02
  • @DiegoCNascimento, I need know which is the ip of the user on internet your public ip, for know of which country are the user – Gatiko06 Jan 29 '14 at 19:03
  • @DiegoCNascimento, but please no sent me negative feedback :( i am newbie – Gatiko06 Jan 29 '14 at 19:05
  • I don't send negative feedback the -1 is not mine. Also marking a question as duplicate is not a negative feedback. – Diego C Nascimento Jan 29 '14 at 19:05
  • @DiegoCNascimento, if you have all this experience, why would you even assume that there's a direct connection without NAT? That's *possible*, but highly unlikely; even mobile networks use NAT now. Secondly, if you're questioning someone's statements without providing the solution you claim you have, you're wasting everyone's time. Lastly, if you don't have time to "search for others," answering questions might not be for you, and your comments look more like trolling. The question is non-trivial, and your "answer" is overly complex, if possible at all. – 323go Jan 29 '14 at 19:05
  • @Gatiko06 you question is to broad, try narrowing it down, like how to know the public IP first, then how to relate that with the possible user geographical location. Also, if you don't know the user infrastructure/scenario, the using some remote service is the way to go, so you need to explain the scenario too. Be aware that this is not fool prof, a man in the middle attack can redirect the requests to the service, or it can be using a proxy. – Diego C Nascimento Jan 29 '14 at 19:25
  • I Only need the public ip of the user later see the country – Gatiko06 Jan 29 '14 at 19:29
  • @Gatiko06 so take a look at this answer http://stackoverflow.com/questions/18254848/getting-ip-in-java/18255093#18255093 and you will have a way to take the public IP bu an external service. After that you can search or ask how to try to get the geographic location of the user by the IP. – Diego C Nascimento Jan 30 '14 at 19:31

1 Answers1

-1
  public String getIpAddr() {
       WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
       WifiInfo wifiInfo = wifiManager.getConnectionInfo();
       nt ip = wifiInfo.getIpAddress();

  String ipString = String.format(
       "%d.%d.%d.%d",
        (ip & 0xff),
        (ip >> 8 & 0xff),
        (ip >> 16 & 0xff),
        (ip >> 24 & 0xff));

        return ipString;

}

Please Note: You need to add android.permission.INTERNET and android.permission.ACCESS_WIFI_STATE in your AndroidManifest.xml as to access the code.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

via Detect wifi IP address on Android? , user Tanmay Mandal

Community
  • 1
  • 1
Amealy
  • 37
  • 7