HTTP助手
public class HttpHelper
extends Object
implements IHttpHelper
java.lang.Object | |
↳ | com.android.tradefed.util.net.HttpHelper |
包含用於發出 http 請求的輔助方法
概括
嵌套類 | |
---|---|
class | HttpHelper.RequestRunnable 可運行以使用 |
公共構造函數 | |
---|---|
HttpHelper () |
公共方法 | |
---|---|
String | buildParameters ( MultiMap <String, String> paramMap) 構建編碼參數字符串。 |
String | buildUrl (String baseUrl, MultiMap <String, String> paramMap) 構建完整的編碼 URL 請求字符串。 |
HttpURLConnection | createConnection (URL url, String method, String contentType) 創建給定的 url。 |
HttpURLConnection | createJsonConnection (URL url, String method) 創建到給定 URL 的連接以傳遞 json 數據。 |
HttpURLConnection | createXmlConnection (URL url, String method) 創建到給定 URL 的連接以傳遞 xml 數據。 |
void | doGet (String url, OutputStream outputStream) 對給定的 URL 執行 GET HTTP 請求方法並將結果流式傳輸到 |
String | doGet (String url) 對給定的 URL 執行 GET HTTP 請求方法並將其作為 |
void | doGetIgnore (String url) 對給定的 URL 執行 GET,給定的 URL 參數忽略結果。 |
void | doGetIgnoreWithRetry (String url) 執行 { |
String | doGetWithRetry (String url) 執行 { |
String | doPostWithRetry (String url, String postData) 對給定的 URL 執行 POST HTTP 請求方法並將其作為 |
String | doPostWithRetry (String url, String postData, String contentType) 對給定的 URL 執行 POST HTTP 請求方法並將其作為 |
int | getInitialPollInterval () 以毫秒為單位獲取初始輪詢間隔。 |
int | getMaxPollInterval () 獲取以毫秒為單位的最大輪詢間隔。 |
int | getMaxTime () 獲取以毫秒為單位繼續嘗試請求的最長時間。 |
int | getOpTimeout () 以毫秒為單位獲取操作超時。 |
IRunUtil | getRunUtil () 獲取 |
void | setInitialPollInterval (int time) 以毫秒為單位設置初始輪詢間隔。 |
void | setMaxPollInterval (int time) 以毫秒為單位設置初始輪詢間隔。 |
void | setMaxTime (int time) 設置以毫秒為單位繼續嘗試請求的最長時間。 |
void | setOpTimeout (int time) 以毫秒為單位設置操作超時。 |
公共構造函數
HTTP助手
public HttpHelper ()
公共方法
構建參數
public String buildParameters (MultiMap<String, String> paramMap)
構建編碼參數字符串。
參數 | |
---|---|
paramMap | MultiMap : URL 參數 |
退貨 | |
---|---|
String | 編碼參數字符串 |
建立網址
public String buildUrl (String baseUrl, MultiMap<String, String> paramMap)
構建完整的編碼 URL 請求字符串。
參數 | |
---|---|
baseUrl | String : 基本 URL |
paramMap | MultiMap : URL 參數 |
退貨 | |
---|---|
String | 構造的 URL |
創建連接
public HttpURLConnection createConnection (URL url, String method, String contentType)
創建給定的 url。
參數 | |
---|---|
url | URL :要連接的ERROR(/URL) 。 |
method | String : HTTP 請求方法。例如,GET 或 POST。 |
contentType | String :內容類型。例如,“文本/html”。 |
退貨 | |
---|---|
HttpURLConnection | HttpURLConnection |
創建JsonConnection
public HttpURLConnection createJsonConnection (URL url, String method)
創建到給定 URL 的連接以傳遞 json 數據。
參數 | |
---|---|
url | URL :要連接的ERROR(/URL) 。 |
method | String : HTTP 請求方法。例如,GET 或 POST。 |
退貨 | |
---|---|
HttpURLConnection | ERROR(/HttpURLConnection) |
創建 XmlConnection
public HttpURLConnection createXmlConnection (URL url, String method)
創建到給定 URL 的連接以傳遞 xml 數據。
參數 | |
---|---|
url | URL :要連接的ERROR(/URL) 。 |
method | String : HTTP 請求方法。例如,GET 或 POST。 |
退貨 | |
---|---|
HttpURLConnection | ERROR(/HttpURLConnection) |
獲取
public void doGet (String url, OutputStream outputStream)
對給定的 URL 執行 GET HTTP 請求方法並將結果流式傳輸到ERROR(/OutputStream)
。
參數 | |
---|---|
url | String :網址 |
outputStream | OutputStream : 響應數據流 |
獲取
public String doGet (String url)
對給定的 URL 執行 GET HTTP 請求方法並將其作為ERROR(/String)
返回。
因為遠程內容被加載到內存中,所以這種方法應該只用於相對較小的數據大小。
參考:
- Java URL 連接: http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html
- Java URL 閱讀器: http://java.sun.com/docs/books/tutorial/networking/urls/readingURL.html
- Java 設置代理: http://java.sun.com/docs/books/tutorial/networking/urls/_setProxy.html
參數 | |
---|---|
url | String :網址 |
退貨 | |
---|---|
String | ERROR(/String) 遠程內容 |
投擲 | |
---|---|
IHttpHelper.DataSizeException |
忽略
public void doGetIgnore (String url)
對給定的 URL 執行 GET,給定的 URL 參數忽略結果。
參數 | |
---|---|
url | String :網址 |
doGetIgnoreWithRetry
public void doGetIgnoreWithRetry (String url)
執行 { doGetIgnore(String)
失敗時重試。
參數 | |
---|---|
url | String :網址 |
doGetWithRetry
public String doGetWithRetry (String url)
執行 { doGet(String)
失敗時重試。
參數 | |
---|---|
url | String :網址 |
退貨 | |
---|---|
String | ERROR(/String) 遠程內容 |
投擲 | |
---|---|
IHttpHelper.DataSizeException |
doPostWithRetry
public String doPostWithRetry (String url, String postData)
對給定的 URL 執行 POST HTTP 請求方法並將其作為ERROR(/String)
返回,失敗時重試。
因為遠程內容被加載到內存中,所以這種方法應該只用於相對較小的數據大小。
參數 | |
---|---|
url | String :網址 |
postData | String : 連接打開後要發布的數據 |
退貨 | |
---|---|
String | ERROR(/String) 遠程內容 |
投擲 | |
---|---|
IHttpHelper.DataSizeException |
doPostWithRetry
public String doPostWithRetry (String url, String postData, String contentType)
對給定的 URL 執行 POST HTTP 請求方法並將其作為ERROR(/String)
返回,失敗時重試。
因為遠程內容被加載到內存中,所以這種方法應該只用於相對較小的數據大小。
參數 | |
---|---|
url | String :網址 |
postData | String : 連接打開後要發布的數據 |
contentType | String :內容類型。例如,“文本/html”。 |
退貨 | |
---|---|
String | ERROR(/String) 遠程內容 |
投擲 | |
---|---|
IHttpHelper.DataSizeException |
getInitialPollInterval
public int getInitialPollInterval ()
以毫秒為單位獲取初始輪詢間隔。
退貨 | |
---|---|
int |
獲取最大輪詢間隔
public int getMaxPollInterval ()
獲取以毫秒為單位的最大輪詢間隔。
退貨 | |
---|---|
int |
獲取最大時間
public int getMaxTime ()
獲取以毫秒為單位繼續嘗試請求的最長時間。
退貨 | |
---|---|
int |
獲取操作超時
public int getOpTimeout ()
以毫秒為單位獲取操作超時。
退貨 | |
---|---|
int |
設置初始輪詢間隔
public void setInitialPollInterval (int time)
以毫秒為單位設置初始輪詢間隔。
參數 | |
---|---|
time | int |
設置最大輪詢間隔
public void setMaxPollInterval (int time)
以毫秒為單位設置初始輪詢間隔。
參數 | |
---|---|
time | int |
設置最大時間
public void setMaxTime (int time)
設置以毫秒為單位繼續嘗試請求的最長時間。
參數 | |
---|---|
time | int |
設置操作超時
public void setOpTimeout (int time)
以毫秒為單位設置操作超時。
參數 | |
---|---|
time | int |
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2023-03-30 (世界標準時間)。