Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤和異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階 Java

Java 其他

Java API 和框架

Java 類引用

Java 有用資源

Java - URLConnection 的 getAllowUserInteraction() 方法



描述

Java URLConnection getAllowUserInteraction() 方法返回此物件的 allowUserInteraction 欄位的值。

宣告

以下是 java.net.URLConnection.getAllowUserInteraction() 方法的宣告

public boolean getAllowUserInteraction()

引數

返回值

此物件的 allowUserInteraction 欄位的值。

異常

示例 1

以下示例演示瞭如何針對具有 https 協議的有效 url 使用 Java URLConnection getAllowUserInteraction() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並打印出來。

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();

         System.out.println(urlConnection.getAllowUserInteraction());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

false

示例 2

以下示例演示瞭如何針對具有 https 協議的有效 url 使用 Java URLConnection getAllowUserInteraction() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。現在,使用 setAllowUserInteraction() 方法,我們將 allowUserInteraction 欄位的值設定為 true,然後使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並打印出來。

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();
         urlConnection.setAllowUserInteraction(true);
         System.out.println(urlConnection.getAllowUserInteraction());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

true

示例 3

以下示例演示瞭如何針對具有 https 協議的有效 url 使用 Java URLConnection getAllowUserInteraction() 方法。在此示例中,我們正在建立 URL 類的例項。使用 url.openConnection() 方法,我們獲得了 URLConnection 例項。現在,使用 setAllowUserInteraction() 方法,我們將 allowUserInteraction 欄位的值設定為 false,然後使用 getAllowUserInteraction(),我們獲取 URLConnection 例項的 allowUserInteraction 欄位的狀態並打印出來。

package com.tutorialspoint;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class UrlConnectionDemo {
   public static void main(String [] args) {
      try {
         URL url = new URL("https://tutorialspoint.tw");
         URLConnection urlConnection = url.openConnection();
         urlConnection.setAllowUserInteraction(false);
         System.out.println(urlConnection.getAllowUserInteraction());

      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

讓我們編譯並執行上述程式,這將產生以下結果:

輸出

false
java_urlconnection.htm
廣告