Android App获取自己的公网IP地址
核心原理
可以通过给
https://ipinfo.io/ip
发送一个请求,这个会解析请求头里的数据,并将其ip解析出来返回到body数据里,这样前端页面拿到这个数据显示出来就可以知道自己设备联网的公网ip地址了。
Android客户端代码
引入okhttp的依赖
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
Java代码
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private TextView ipTextView;
private Handler mainHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ipTextView = findViewById(R.id.ipTextView);
// 创建一个主线程的 Handler,接收并处理子线程发送的消息
mainHandler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 处理消息
if (msg.what == 1) {
String publicIp = (String) msg.obj; // 获取传递过来的公网 IP
ipTextView.setText("Public IP: " + publicIp); // 更新 UI
}
}
};
findViewById(R.id.btnGetPublicIP).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 启动子线程进行网络请求
new Thread(new Runnable() {
@SuppressLint("SetTextI18n")
@Override
public void run() {
String ip = getIp();
if (!TextUtils.isEmpty(ip)) {
// 子线程发送消息到主线程
Message msg = mainHandler.obtainMessage();
msg.what = 1; // 消息标识
msg.obj = ip; // 传递结果
mainHandler.sendMessage(msg); // 发送消息到主线程
}
}
}).start();
}
});
}
public String getIp() {
String ip = "";
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("https://ipinfo.io/ip")
.build();
try {
Response response = okHttpClient.newCall(request).execute();
if (response.isSuccessful()) {
if (response.body() != null) {
String requestBody = response.body().string();
Log.i("MainActivity", "requestBody length is " + requestBody.length());
ip = requestBody;
return ip;
}
}
} catch (Exception e) {
Log.e("MainActivity", "ipAddress is null exception is " + e);
}
return ip;
}
}
引入布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/btnGetPublicIP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Public IP"
android:layout_alignParentTop="true"/>
<TextView
android:id="@+id/ipTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Public IP will be shown here"
android:layout_below="@id/btnGetPublicIP"
android:layout_marginTop="16dp"/>
</RelativeLayout>
运行结果
原文地址:https://blog.csdn.net/qq_41688840/article/details/143881069
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!