Verify Android Current IP Address

Asked 2 years ago, Updated 2 years ago, 37 views

Using the socket class, I want to check the ip address that is currently connected regardless of Wi-Fi or LTE.


try {
                    Socket mysocket = new Socket("www.google.com", 80);
                    String Addr = mysocket.getLocalAddress().getHostAddress();
                    tv8.setText(Addr);
                }catch (Exception e){
                    Log.e("TAG",e.getMessage());
                }

The application is forced to terminate when the code is executed, and the log does not appear May I know what the problem is?

java android-studio

2022-09-21 09:58

1 Answers

MainThread is unable to work with the network.

Make socket communication after creating a separate Thread.


2022-09-21 09:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.