相关疑难解决方法(0)

如何在Java中解析JSON

我有以下JSON文本.如何可以解析它来获得pageName,pagePic,post_id,等?

{
   "pageInfo": {
         "pageName": "abc",
         "pagePic": "http://example.com/content.jpg"
    },
    "posts": [
         {
              "post_id": "123456789012_123456789012",
              "actor_id": "1234567890",
              "picOfPersonWhoPosted": "http://example.com/photo.jpg",
              "nameOfPersonWhoPosted": "Jane Doe",
              "message": "Sounds cool. Can't wait to see it!",
              "likesCount": "2",
              "comments": [],
              "timeOfPost": "1234567890"
         }
    ]
}
Run Code Online (Sandbox Code Playgroud)

java parsing json

971
推荐指数
26
解决办法
154万
查看次数

使用Java获取当前计算机的IP地址

我正在尝试开发一个系统,其中有不同的节点在不同的系统或同一系统上的不同端口上运行.

现在,所有节点都创建一个Socket,其目标IP作为称为引导节点的特殊节点的IP.然后节点创建自己的节点ServerSocket并开始侦听连接.

引导节点维护一个节点列表,并在被查询时返回它们.

现在我需要的是节点必须将其IP注册到自举节点.我尝试使用cli.getInetAddress()一旦客户端连接到ServerSocketbootstrapping节点,但这不起作用.

  1. 我需要客户端注册其PPP IP(如果可用);
  2. 否则LAN IP如果可用;
  3. 否则它必须注册127.0.0.1,假设它是同一台计算机.

使用代码:

System.out.println(Inet4Address.getLocalHost().getHostAddress());
Run Code Online (Sandbox Code Playgroud)

要么

System.out.println(InetAddress.getLocalHost().getHostAddress());
Run Code Online (Sandbox Code Playgroud)

我的PPP连接IP地址是:117.204.44.192,但上面的返回值为192.168.1.2

编辑

我使用以下代码:

Enumeration e = NetworkInterface.getNetworkInterfaces();
while(e.hasMoreElements())
{
    NetworkInterface n = (NetworkInterface) e.nextElement();
    Enumeration ee = n.getInetAddresses();
    while (ee.hasMoreElements())
    {
        InetAddress i = (InetAddress) ee.nextElement();
        System.out.println(i.getHostAddress());
    }
}
Run Code Online (Sandbox Code Playgroud)

我能够获得所有相关的所有IP地址NetworkInterface,但我如何区分它们?这是我得到的输出:

127.0.0.1
192.168.1.2
192.168.56.1
117.204.44.19
Run Code Online (Sandbox Code Playgroud)

java sockets ip

274
推荐指数
10
解决办法
50万
查看次数

如何以编程方式查找Android设备的MAC地址

我如何通过编程方式获取Android设备的Mac Id.我已经完成了IMIE Code,我知道如何手动检查设备上的Mac ID,但不知道如何以编程方式查找.

android

49
推荐指数
5
解决办法
9万
查看次数

获取我的wifi ip地址Android

如何在wifi下连接时获取手机的IP地址?

我在这里找到了一个方法,但它返回了类似24.182.239.255的东西,即使我在wifi下,我期待像192.168.1.10这样的东西.

我喜欢这样的东西:

if (you are under wifi)
    String ip4 = getWifiIP()
else
    String ip4 = getIPAddress with the method linked before
Run Code Online (Sandbox Code Playgroud)

非常感谢!

ip android android-networking android-wifi

39
推荐指数
4
解决办法
8万
查看次数

你如何获得Android的当前DNS服务器?

我正试图抓住我的应用程序中当前使用的DNS服务器的地址,要么我通过Wifi或移动连接.DhcpInfo对象应提供此功能,但如何获取有效的DhcpInfo对象?

android

26
推荐指数
4
解决办法
3万
查看次数

获取没有Wifi的Android设备的MAC地址

如何获取没有Wifi接口的android设备的网络接口的MAC地址(例如android模拟器)?通过WifiManager获得的WifiInfo返回null.

编辑

更清楚一点:我必须在本地网络上与现有网络协议(不是由我设计)进行通信,在注册阶段我必须在有效负载内发送通信接口的mac地址.

android mac-address

21
推荐指数
3
解决办法
5万
查看次数

Android API-23:InetAddressUtils替换

切换到Android棉花糖API,我使用org.apache.http.conn.util.InetAddressUtilsInetAddressUtils.isIPv4Address(ipAddress)一个代码,列出从设备的所有IP.

作为API-23更改的一部分,该InetAddressUtils课程现已消失.

我现在如何替换以下代码?

public static String ipAddress() {
    try {
        for (final Enumeration<NetworkInterface> enumerationNetworkInterface = NetworkInterface.getNetworkInterfaces(); enumerationNetworkInterface.hasMoreElements();) {
            final NetworkInterface networkInterface = enumerationNetworkInterface.nextElement();
            for (Enumeration<InetAddress> enumerationInetAddress = networkInterface.getInetAddresses(); enumerationInetAddress.hasMoreElements();) {
                final InetAddress inetAddress = enumerationInetAddress.nextElement();
                final String ipAddress = inetAddress.getHostAddress();
                if (! inetAddress.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipAddress)) {
                    return ipAddress;
                }
            }
        }
        return null;
    }
    catch (final Exception e) {
        LogHelper.wtf(null, e);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

android android-networking android-6.0-marshmallow

19
推荐指数
2
解决办法
1万
查看次数

Runtime.exec()bug:挂起而不提供Process对象

我是否用这个:

process = Runtime.getRuntime().exec("logcat -d time");
Run Code Online (Sandbox Code Playgroud)

或者那个:

process = new ProcessBuilder()
              .command("logcat", "-d", "time")
              .redirectErrorStream(true)
              .start();
Run Code Online (Sandbox Code Playgroud)

我得到了相同的结果:它经常挂在exec()或start()调用中,无论我试图做什么!运行它的线程甚至不能被Thread.interrupt()中断!子进程肯定已启动,如果被杀死,则返回上述命令.

这些调用可能在第一次尝试时失败,所以没有办法阅读他们的输出!我也可以用一个简单的"su -c kill xxx"命令行,结果相同!

编辑:开始使用一些调试日志调试NDK项目中的java_lang_ProcessManager.cpp文件!所以这是我到目前为止找到的,在fork()之后父进程执行此操作:

int result;
int count = read(statusIn, &result, sizeof(int));            <- hangs there
close(statusIn);
Run Code Online (Sandbox Code Playgroud)

虽然子进程不应该阻止它:这就是孩子所做的事情(如果开始的话!):

    // Make statusOut automatically close if execvp() succeeds.
    fcntl(statusOut, F_SETFD, FD_CLOEXEC);                      <- make the parent will not block

    // Close remaining unwanted open fds.
    closeNonStandardFds(statusOut, androidSystemPropertiesFd);  <- hangs here sometimes

    ...

    execvp(commands[0], commands);

    // If we got here, execvp() failed or the working …
Run Code Online (Sandbox Code Playgroud)

android process exec

18
推荐指数
2
解决办法
4330
查看次数

如何在Android中将设备连接到WiFi时获取蜂窝网络的IP地址

有没有办法我可以同时获取Android中的WiFi和蜂窝网络的IP地址.我尝试使用很多例子,但能够获得只有WiFi网络而不是蜂窝网络的地址.我已启用WiFi和蜂窝网络设备可通过WiFi访问Internet.

这是我用来获取IP地址的代码:

    String ipAddress = null;
    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()) {
                    ipAddress = inetAddress.getHostAddress().toString();
                    Log.i("Here is the Address",ipAddress);
                }
            }
        }
    } catch (SocketException ex) {

    }
Run Code Online (Sandbox Code Playgroud)

当设备连接到WiFi时,是否有可能获得蜂窝网络的IP地址.如果是,那么这是可行的.

networking android ip-address

18
推荐指数
2
解决办法
1万
查看次数

Formatter.formatIpAddress(int)的替代方案;

我在我的应用程序中使用了这段代码,但警告说: "不推荐使用Formatter类型的formatIpAddress(int)方法"

android.text.format.Formatter.formatIpAddress(mWifiManager.getConnectionInfo().getIpAddress());
Run Code Online (Sandbox Code Playgroud)

什么是快速解决方案?

android

16
推荐指数
3
解决办法
1万
查看次数