package com.duqing.missions.util;
|
|
import android.annotation.SuppressLint;
|
import android.content.Context;
|
import android.net.ConnectivityManager;
|
import android.net.NetworkInfo;
|
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiManager;
|
import android.util.Log;
|
|
import org.json.JSONException;
|
import org.json.JSONObject;
|
|
import java.io.BufferedReader;
|
import java.io.IOException;
|
import java.io.InputStream;
|
import java.io.InputStreamReader;
|
import java.net.HttpURLConnection;
|
import java.net.Inet6Address;
|
import java.net.InetAddress;
|
import java.net.MalformedURLException;
|
import java.net.NetworkInterface;
|
import java.net.SocketException;
|
import java.net.URL;
|
import java.net.URLConnection;
|
import java.util.Enumeration;
|
import java.util.HashMap;
|
import java.util.Map;
|
|
/**
|
* My father is Object, ites purpose of
|
*
|
* @purpose Created by Runt (qingingrunt2010@qq.com) on 2019-3-19.
|
*/
|
|
public class NetWorkUtils {
|
|
|
/**
|
* 获取外网IP地址
|
* @return
|
*/
|
public static String getNetIp() {
|
final Map<String,String> param = new HashMap<>();
|
Thread thread = new Thread(new Runnable() {
|
@Override
|
public void run() {
|
String line = "";
|
URL infoUrl = null;
|
InputStream inStream = null;
|
try {
|
infoUrl = new URL("http://pv.sohu.com/cityjson?ie=utf-8");
|
URLConnection connection = infoUrl.openConnection();
|
HttpURLConnection httpConnection = (HttpURLConnection) connection;
|
int responseCode = httpConnection.getResponseCode();
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
inStream = httpConnection.getInputStream();
|
BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
|
StringBuilder strber = new StringBuilder();
|
while ((line = reader.readLine()) != null)
|
strber.append(line + "\n");
|
inStream.close();
|
// 从反馈的结果中提取出IP地址
|
int start = strber.indexOf("{");
|
int end = strber.indexOf("}");
|
String json = strber.substring(start, end + 1);
|
if (json != null) {
|
try {
|
JSONObject jsonObject = new JSONObject(json);
|
line = jsonObject.optString("cip");
|
} catch (JSONException e) {
|
e.printStackTrace();
|
}
|
}
|
param.put("ip",line);
|
}
|
} catch (MalformedURLException e) {
|
e.printStackTrace();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
});
|
try {
|
thread.start();
|
thread.join();
|
} catch (InterruptedException e) {
|
e.printStackTrace();
|
}
|
return param.get("ip");
|
}
|
|
/***
|
* 获取局域网ip
|
* @param context
|
* @return
|
*/
|
@SuppressLint("MissingPermission")
|
public static String getLocalIpAddress(Context context) {
|
|
ConnectivityManager connect = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
//检查网络连接
|
NetworkInfo info = connect.getActiveNetworkInfo();
|
|
if (info == null) {
|
return "";
|
}
|
int netType = info.getType();
|
int netSubtype = info.getSubtype();
|
|
if (netType == ConnectivityManager.TYPE_WIFI) { //WIFI
|
return getWifiIpAddress(context);
|
} else /*if (netType == ConnectivityManager.TYPE_MOBILE) { //MOBILE
|
return getGPRSIpAddress();
|
} else */if (netType == ConnectivityManager.TYPE_ETHERNET) { //MOBILE
|
return getEthernetIp();
|
} else {
|
return "";
|
}
|
}
|
|
/**
|
* 使用wifi
|
* @param context
|
* @return
|
*/
|
@SuppressLint("MissingPermission")
|
public static String getWifiIpAddress(Context context) {
|
|
//获取wifi服务
|
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
|
//判断wifi是否开启
|
// if (!wifiManager.isWifiEnabled()) {
|
// <span style="white-space:pre"> </span> wifiManager.setWifiEnabled(true);
|
// }
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
int ipAddress = wifiInfo.getIpAddress();
|
String ip = intToIp(ipAddress);
|
return ip;
|
}
|
|
private static String intToIp(int i) {
|
return (i & 0xFF) + "." +
|
((i >> 8) & 0xFF) + "." +
|
((i >> 16) & 0xFF) + "." +
|
(i >> 24 & 0xFF);
|
}
|
/**
|
* 使用GPRS
|
* @return
|
*/
|
public static String getGPRSIpAddress() {
|
try {
|
for (Enumeration<NetworkInterface> en = NetworkInterface
|
.getNetworkInterfaces(); en.hasMoreElements(); ) {
|
NetworkInterface intf = en.nextElement();
|
for (Enumeration<InetAddress> enumIpAddr = intf
|
.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
|
InetAddress inetAddress = enumIpAddr.nextElement();
|
if (!inetAddress.isLoopbackAddress()) {
|
return inetAddress.getHostAddress().toString();
|
}
|
}
|
}
|
} catch (SocketException ex) {
|
MyLog.e("Exception", ex.toString());
|
}
|
return "127.0.0.1";
|
}
|
|
public static String getEthernetIp() {
|
String hostIp = null;
|
try {
|
Enumeration nis = NetworkInterface.getNetworkInterfaces();
|
InetAddress ia = null;
|
while (nis.hasMoreElements()) {
|
NetworkInterface ni = (NetworkInterface) nis.nextElement();
|
Enumeration<InetAddress> ias = ni.getInetAddresses();
|
while (ias.hasMoreElements()) {
|
ia = ias.nextElement();
|
if (ia instanceof Inet6Address) {
|
continue;// skip ipv6
|
}
|
String ip = ia.getHostAddress();
|
if (!"127.0.0.1".equals(ip)) {
|
hostIp = ia.getHostAddress();
|
break;
|
}
|
}
|
}
|
} catch (SocketException e) {
|
Log.i("yao", "SocketException");
|
e.printStackTrace();
|
}
|
return hostIp;
|
}
|
|
|
/**
|
* 将得到的int类型的IP转换为String类型
|
*
|
* @param ip
|
* @return
|
*/
|
public static String intIP2StringIP(int ip) {
|
return (ip & 0xFF) + "." +
|
((ip >> 8) & 0xFF) + "." +
|
((ip >> 16) & 0xFF) + "." +
|
(ip >> 24 & 0xFF);
|
}
|
|
}
|