This is the easiest and simple way ever exist on the internet... First of all, add this permission to your manifest file...
"INTERNET"
"ACCESS_NETWORK_STATE"
add this in onCreate file of Activity..
getPublicIP();
Now Add this function to your MainActivity.class.
private void getPublicIP() {_x000D_
ArrayList<String> urls=new ArrayList<String>(); //to read each line_x000D_
_x000D_
new Thread(new Runnable(){_x000D_
public void run(){_x000D_
//TextView t; //to show the result, please declare and find it inside onCreate()_x000D_
_x000D_
try {_x000D_
// Create a URL for the desired page_x000D_
URL url = new URL("https://api.ipify.org/"); //My text file location_x000D_
//First open the connection_x000D_
HttpURLConnection conn=(HttpURLConnection) url.openConnection();_x000D_
conn.setConnectTimeout(60000); // timing out in a minute_x000D_
_x000D_
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));_x000D_
_x000D_
//t=(TextView)findViewById(R.id.TextView1); // ideally do this in onCreate()_x000D_
String str;_x000D_
while ((str = in.readLine()) != null) {_x000D_
urls.add(str);_x000D_
}_x000D_
in.close();_x000D_
} catch (Exception e) {_x000D_
Log.d("MyTag",e.toString());_x000D_
}_x000D_
_x000D_
//since we are in background thread, to post results we have to go back to ui thread. do the following for that_x000D_
_x000D_
PermissionsActivity.this.runOnUiThread(new Runnable(){_x000D_
public void run(){_x000D_
try {_x000D_
Toast.makeText(PermissionsActivity.this, "Public IP:"+urls.get(0), Toast.LENGTH_SHORT).show();_x000D_
}_x000D_
catch (Exception e){_x000D_
Toast.makeText(PermissionsActivity.this, "TurnOn wiffi to get public ip", Toast.LENGTH_SHORT).show();_x000D_
}_x000D_
}_x000D_
});_x000D_
_x000D_
}_x000D_
}).start();_x000D_
_x000D_
}
_x000D_