简体   繁体   English

在Java中执行URI命令

[英]Executing URI commands in Java

One way that Steam lets users launch games and perform many other operations, is by using URI protocols, for example (from Valve developer community ): Steam允许用户启动游戏并执行许多其他操作的一种方法是使用URI协议(例如来自Valve开发人员社区 ):

steam://run/<id> will launch the game that corresponds to the specified ID. steam://run/<id>将启动与指定ID对应的游戏。

steam://validate/<id> will validate the game files of the specified ID. steam://validate/<id>将验证指定ID的游戏文件。

How can I get Java to 'run' these? 如何让Java“运行”这些? I don't even know what you call it, ie do you 'run' URIs, or 'execute' them, or what? 我甚至不知道你叫什么,即你'运行'URI,或'执行'它们,或者什么? Because persumably these URIs don't have anything to return, and the URI class in Java doesn't have anything related to 'executing' them, however URL does, but it doesn't work! 因为可能这些URI没有任何东西可以返回,并且Java中的URI类没有任何与“执行”它们相关的东西,但URL确实如此,但它不起作用!

I've tried this: 我试过这个:

...
try
{
    URI testURI = URI.create("steam://run/240");
    URL testURL = joinURI.toURL();
    // URL testURL = new URL("steam://run/240") doesn't work either
    joinURL.openConnection(); // Doesn't work
    // joinURL.openStream() doesn't work either
}
catch (MalformedURLException e)
{
    System.err.println(e.getMessage());
}
...

Each combination gives the error: unknown protocol: steam . 每个组合都给出错误: unknown protocol: steam

The system that Steam uses to handle the URIs is definitely working, because for example, I can type the above URI into Firefox and it works. Steam用来处理URI的系统肯定是有效的,因为例如,我可以在Firefox中输入上面的URI并且它可以工作。

My eternal gratitude to the person who provides the answer! 我永远感谢提供答案的人!

Thanks 谢谢

Try Desktop.browse(URI) , this should start the "default action" which is the Steam client for a steam:// URI, eg 尝试使用Desktop.browse(URI) ,这应该启动“默认操作”,它是Steam steam:// URI的Steam客户端,例如

URI uri = new URI("steam://store/240");
if (Desktop.isDesktopSupported()) {
    Desktop.getDesktop().browse(uri);
}

The system that Steam uses to handle the URIs is definitely working, because for example, I can type the above URI into Firefox and it works. Steam用来处理URI的系统肯定是有效的,因为例如,我可以在Firefox中输入上面的URI并且它可以工作。

It is working because Firefox (or other browsers) can associate unkown protocols with applications. 它的工作原理是因为Firefox(或其他浏览器)可以将未知协议与应用程序相关联。 When you load steam://xxx for the first time, Firefox asks you which application you want to open. 当您第一次加载steam://xxx时,Firefox会询问您要打开哪个应用程序。 If it didn't ask you, steam probably installed a browser plugin for that. 如果它没有问你,steam可能会安装一个浏览器插件。

A Uniform Resource Identifier (URI) just identifies a resource, it doesn't necessarily describe how to access it. 统一资源标识符(URI)仅标识资源,它不一定描述如何访问它。 Moreover, for custom protocols, such as "steam" the vendor can define any underlying access conventions which compatible client programs must know to interact. 此外,对于自定义协议,例如“steam”,供应商可以定义兼容的客户端程序必须知道的任何底层访问约定以进行交互。

In order to "execute" a URI like this you need to know exactly how the protocol is implemented (is it over HTTP? TCP? UDP?) and how to speak with the server at the other end. 为了“执行”这样的URI,您需要确切地知道协议是如何实现的(是通过HTTP?TCP?UDP?)以及如何与另一端的服务器通信。

The Valve Developer Community wiki page might have some useful information. Valve Developer社区维基页面可能包含一些有用的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM