Advertisement
solodroid

Get Public IP

Jun 25th, 2024 (edited)
507
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | Source Code | 0 0
  1.     public static class GetPublicIP extends AsyncTask<String, String, String> {
  2.  
  3.         @Override
  4.         protected String doInBackground(String... strings) {
  5.             String publicIP = "";
  6.             try {
  7.                 java.util.Scanner s = new java.util.Scanner(new java.net.URL("https://api.ipify.org")
  8.                         .openStream(), "UTF-8")
  9.                         .useDelimiter("\\A");
  10.                 publicIP = s.next();
  11.                 Log.d("TAG", "My current IP address is " + publicIP);
  12.             } catch (java.io.IOException e) {
  13.                 e.printStackTrace();
  14.             }
  15.  
  16.             return publicIP;
  17.         }
  18.  
  19.         @Override
  20.         protected void onPostExecute(String publicIp) {
  21.             super.onPostExecute(publicIp);
  22.             Log.d("TAG", publicIp + "");
  23.             //Here 'publicIp' is your desire public IP
  24.         }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement