2017-10-20 29 views
-1

我想在一個特定的API HTTP調用傳遞「主機名」「端口」,「集羣」。如何傳遞網址中的變量? 任何幫助,非常感謝。在Java中傳遞API url中的變量。

代碼:

\t int port = Integer.parseInt(prop.getProperty("sd.port")); 
 
\t System.out.println("port no : " + port); 
 
\t String hostName = prop.getProperty("sd.hostname"); 
 
\t System.out.println("host name : " + hostName); 
 
\t 
 
\t String cluster = app_prop.getProperty("ClusterName"); 
 
\t System.out.println("cluster name is : " + cluster); 
 
\t 
 
\t 
 
\t 
 
\t try { 
 

 
\t \t 
 
\t String url ="https://hostName:port/api/scanCluster?cid=cluster&mode=1";

在上述網址的看到,我試圖通過 「主機名」, 「端口」 和 「集羣」。請幫忙 。

+1

您已經使用帶有'+ port','+ hostName',附加的字符串的例子'+ cluster'而且你只需要爲網址做到這一點。 –

回答

3

使用字符串連接(與+運營商)以字符串常量和變量結合:

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

備註:很多人說,你應該使用StringBuffer/StringBuilder而不是+ concatination。這僅適用於在循環或更復雜的構造中構建字符串時的情況。在這種情況下,你可以絕對地使用+ concatination,因爲編譯器會優化代碼並使用StringBuilder。你可以通過檢查ByteCode來查看。 – Korashen

+0

@ f1sh謝謝。我爲自己感到羞愧。對我而言,這是愚蠢的。 – jane

+0

@Korashen感謝您的信息。 – jane