Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中的API url中传递变量。_Java_Rest_Api - Fatal编程技术网

在java中的API url中传递变量。

在java中的API url中传递变量。,java,rest,api,Java,Rest,Api,我想在特定的API HTTP调用中传递“主机名”、“端口”、“集群”。如何在url中传递变量? 非常感谢您的帮助 代码: int-port=Integer.parseInt(prop.getProperty(“sd.port”); System.out.println(“端口号:+端口”); 字符串hostName=prop.getProperty(“sd.hostName”); System.out.println(“主机名:“+hostName”); 字符串cluster=app_prop

我想在特定的API HTTP调用中传递“主机名”“端口”、“集群”。如何在url中传递变量? 非常感谢您的帮助

代码:

int-port=Integer.parseInt(prop.getProperty(“sd.port”);
System.out.println(“端口号:+端口”);
字符串hostName=prop.getProperty(“sd.hostName”);
System.out.println(“主机名:“+hostName”);
字符串cluster=app_prop.getProperty(“ClusterName”);
System.out.println(“集群名称为:“+cluster”);
试一试{
字符串url=”https://hostName:port/api/scanCluster?cid=cluster&mode=1“;
使用字符串连接(使用
+
运算符)组合字符串文字和变量:

String url = "https://"+hostname+":"+port+"/api/scanCluster?cid="+cluster+"&mode=1";

您已经在使用带有
+端口
+主机名
+集群
的字符串追加示例,并且您只需要对url执行该操作。旁注:许多人说,您应该使用StringBuffer/StringBuilder而不是+concatation。这仅适用于在循环中或更复杂的情况下构建字符串构造。在这种情况下,您完全可以使用+concatation,因为编译器将优化代码并使用StringBuilder。您可以通过检查字节码看到这一点。@f1sh非常感谢。我为自己感到羞愧。这对我来说是愚蠢的。