Package com.google.common.net

Guava库的com.google.common.net包提供了处理网络地址的工具,如InternetDomainName和InetAddresses。示例展示了如何验证域名是否为有效的顶级域名以及解析和验证IP地址的有效性。通过InternetDomainName.from()和InetAddresses.forString()方法进行解析,再利用isPublicSuffix()和isInetAddress()进行验证。

com.google.common.net 是 Google Guava 库的一部分,它包含了一些实用的类和方法,用于处理网络地址(如 IP 地址和域名)。这个包中的一些主要组件包括:

  • InternetDomainName:这是一个不可变的、格式良好的互联网域名,例如 comfoo.co.uk
    该包中的其他实用方法和类通常用于处理 IP 地址和域名的各种操作,例如验证、解析和格式化。
    如果你需要更具体的信息或示例代码,可以查阅 Guava 库的官方文档或相关的在线教程。下面是一个简单的示例,演示如何使用 com.google.common.net.InternetDomainName 类来检查一个域名是否是有效的顶级域名(TLD):
import com.google.common.net.InternetDomainName;
public class DomainValidator {
    public static void main(String[] args) {
        String domain = "example.com";
        try {
            InternetDomainName tld = InternetDomainName.from(domain);
            System.out.println("The TLD is: " + tld.topPrivateDomain());
            if (tld.isPublicSuffix()) {
                System.out.println("This is a valid TLD.");
            } else {
                System.out.println("This is not a valid TLD.");
            }
        } catch (IllegalArgumentException e) {
            System.out.println("Invalid domain name: " + domain);
        }
    }
}

在上面的示例中,我们使用 InternetDomainName.from() 方法将域名解析为一个 InternetDomainName 实例。然后,我们可以使用 topPrivateDomain() 方法获取顶级私有域名,并使用 isPublicSuffix() 方法检查域名是否是有效的公共后缀。如果域名无效,from() 方法将抛出 IllegalArgumentException 异常。当然可以,下面是一个更复杂的示例,演示如何使用 com.google.common.net 包中的类来解析和验证 IP 地址:

import com.google.common.net.InetAddresses;
public class IPValidator {
    public static void main(String[] args) {
        String ipAddress = "192.168.1.1";
        try {
            // 解析 IP 地址
            byte[] bytes = InetAddresses.forString(ipAddress);
            // 验证 IP 地址
            if (InetAddresses.isInetAddress(bytes)) {
                System.out.println("Valid IP address: " + ipAddress);
            } else {
                System.out.println("Invalid IP address: " + ipAddress);
            }
        } catch (IllegalArgumentException e) {
            System.out.println("Invalid IP address: " + ipAddress);
        }
    }
}

在上面的示例中,我们使用 InetAddresses.forString() 方法将 IP 地址解析为字节数组。然后,我们使用 InetAddresses.isInetAddress() 方法验证 IP 地址是否有效。如果 IP 地址无效,forString() 方法将抛出 IllegalArgumentException 异常。
This package contains utility methods and classes for working with net addresses (numeric IP and domain names).

See:
Description

Class Summary
HostAndPort HostAndPort performs high-level segmenting of host:port strings.
HostSpecifier A syntactically valid host specifier, suitable for use in a URI.
InetAddresses Static utility methods pertaining to InetAddress instances.
InetAddresses.TeredoInfo A simple data class to encapsulate the information to be found in a Teredo address.
InternetDomainName An immutable well-formed internet domain name, such as com or foo.co.uk.

Package com.google.common.net Description

This package contains utility methods and classes for working with net addresses (numeric IP and domain names).

This package is a part of the open-source Guava libraries.

Author:
Craig Berry
在这里插入图片描述

package com.example.myapplication; import android.net.Uri; import android.os.Bundle; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import androidx.fragment.app.FragmentManager; import com.google.ar.core.Anchor; import com.google.ar.core.Session; // 确保导入正确的Session类 import com.google.ar.sceneform.AnchorNode; import com.google.ar.sceneform.ArSceneView; import com.google.ar.sceneform.rendering.ModelRenderable; import com.google.ar.sceneform.ux.ArFragment; import com.google.ar.sceneform.ux.TransformableNode; public class ArDisplayActivity extends AppCompatActivity { ArFragment arFragment= (ArFragment)getSupportFragmentManager().findFragmentById(R.id.ar_fragment); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ar_display); // 正确从布局中获取ArFragment if (arFragment != null) { // 点击平面时加载并放置3D模型 arFragment.setOnTapArPlaneListener((hitResult, plane, motionEvent) -> { ModelRenderable.builder() .setSource(this, Uri.parse("models/text.glb")) .build() .thenAccept(renderable -> { // 放置模型逻辑 Anchor anchor = hitResult.createAnchor(); AnchorNode anchorNode = new AnchorNode(anchor); anchorNode.setParent(arFragment.getArSceneView().getScene()); TransformableNode modelNode = new TransformableNode(arFragment.getTransformationSystem()); modelNode.setRenderable(renderable); modelNode.setParent(anchorNode); modelNode.select(); // 允许缩放、旋转 }) .exceptionally(throwable -> { Log.e("AR模型加载", "失败:" + throwable.getMessage()); return null; }); }); } else { Log.e("AR错误", "未找到布局中的ar_fragment,请检查ID是否为@+id/ar_fragment"); } } @Override protected void onResume() { super.onResume(); if (arFragment != null) { ArSceneView arSceneView = arFragment.getArSceneView(); if (arSceneView != null) { Session session = arSceneView.getSession(); // 后续可对session进行操作(如检查ARCore支持、配置等) } } } }报错ArFragment arFragment= (ArFragment)getSupportFragmentManager().findFragmentById(R.id.ar_fragment);类型不匹配
最新发布
10-23
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bol5261

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值