當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java URI getQuery()用法及代碼示例


getQuery()函數是URI類的一部分。函數getQuery()返回指定URI的查詢。

函數簽名

public String getQuery()

用法


uri.getQuery()

參數此函數不需要任何參數

返回類型:函數返回字符串類型

以下示例程序旨在說明getQuery()函數的用法:

範例1:給定一個URI,我們將使用getQuery()函數獲取查詢。

// Java program to show the use of the function getQuery() 
  
import java.net.*; 
  
class Solution { 
    public static void main(String args[]) 
    { 
        // uri  object 
        URI uri = null; 
  
        try { 
            // create a URI 
            uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol"); 
  
            // get the  Query 
            String _Query = uri.getQuery(); 
  
            // display the URL 
            System.out.println("URI = " + uri); 
  
            // display the  Query 
            System.out.println(" Query= " + _Query); 
        } 
        // if any error occurs 
        catch (Exception e) { 
            // display the error 
            System.out.println(e); 
        } 
    } 
}
輸出:
URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol
 Query= title=protocol

範例2:現在,我們將不提供任何查詢,而使用該函數來獲取查詢並查看結果。

// Java program to show the use of the function getQuery() 
import java.net.*; 
class Solution { 
    public static void main(String args[]) 
    { 
        // uri  object 
        URI uri = null; 
  
        try { 
            // create a URI 
            uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples"); 
  
            // get the  Query 
            String _Query = uri.getQuery(); 
  
            // display the URL 
            System.out.println("URI = " + uri); 
  
            // display the  Query 
            System.out.println(" Query=" + _Query); 
        } 
        // if any error occurs 
        catch (URISyntaxException e) { 
            // display the error 
            System.out.println("URI Exception =" + e.getMessage()); 
        } 
    } 
}
輸出:
URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples
 Query=null

範例3:getQuery()和getRawQuery()返回的值是相同的,隻是對所有轉義八位位組的序列進行了解碼。
getRawPath()返回用戶提供的字符串的確切值,但是getQuery()函數解碼轉義八位字節的序列(如果有)。

// Java program to show the use of the function getQuery() 
import java.net.*; 
class Solution { 
    public static void main(String args[]) 
    { 
        // uri  object 
        URI uri = null; 
  
        try { 
            // create a URI 
            uri = new URI("https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol%E2%82%AC"); 
  
            // get the  Query 
            String _Query = uri.getQuery(); 
  
            // display the URL 
            System.out.println("URI = " + uri); 
  
            // display the  Query 
            System.out.println(" Query=" + _Query); 
  
            // get the  Raw Query 
            String Raw_Query = uri.getRawQuery(); 
  
            // display the Raw Query 
            System.out.println(" Raw Query=" + Raw_Query); 
        } 
        // if any error occurs 
        catch (URISyntaxException e) { 
            // display the error 
            System.out.println("URI Exception =" + e.getMessage()); 
        } 
    } 
}
輸出:
URI = https://www.geeksforgeeks.org/url-getprotocol-method-in-java-with-examples?title=protocol%E2%82%AC
 Query=title=protocol?
 Raw Query=title=protocol%E2%82%AC


相關用法


注:本文由純淨天空篩選整理自andrew1234大神的英文原創作品 URI getQuery() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。