HttpHelper

public class HttpHelper
extends Object implements IHttpHelper

java.lang.Object
   ↳ com.android.tradefed.util.net.HttpHelper


包含發出 HTTP 要求的輔助方法

摘要

巢狀類別

class HttpHelper.RequestRunnable

Runnable,用於提出包含 IRunUtil.runEscalatingTimedRetry(long,long,long,long,IRunnableResult) 的要求。 

公用建構函式

HttpHelper()

公用方法

String buildParameters(MultiMap<String, String> paramMap)

建構已編碼的參數字串。

String buildUrl(String baseUrl, MultiMap<String, String> paramMap)

建構完整的編碼網址要求字串。

HttpURLConnection createConnection(URL url, String method, String contentType)

Create a to given url.

HttpURLConnection createJsonConnection(URL url, String method)

Creates a connection to given URL for passing json data.

HttpURLConnection createXmlConnection(URL url, String method)

Creates a connection to given URL for passing xml data.

String doGet(String url)

針對指定網址執行 GET HTTP 要求方法,並以 String 形式傳回。

void doGet(String url, OutputStream outputStream)

針對指定網址執行 GET HTTP 要求方法,並將結果串流至 OutputStream

void doGetIgnore(String url)

針對指定網址執行 GET,並忽略結果 (使用指定網址參數)。

void doGetIgnoreWithRetry(String url)

Performs {doGetIgnore(String) retrying upon failure.

String doGetWithRetry(String url)

Performs {doGet(String) retrying upon failure.

String doPostWithRetry(String url, String postData)

針對指定網址執行 POST HTTP 要求方法,並以 String 形式傳回,失敗時會重試。

String doPostWithRetry(String url, String postData, String contentType)

針對指定網址執行 POST HTTP 要求方法,並以 String 形式傳回,失敗時會重試。

int getInitialPollInterval()

以毫秒為單位取得初始輪詢間隔。

int getMaxPollInterval()

以毫秒為單位取得輪詢間隔上限。

int getMaxTime()

以毫秒為單位,取得重試要求的時間上限。

int getOpTimeout()

以毫秒為單位取得作業逾時時間。

IRunUtil getRunUtil()

取得要使用的 IRunUtil

void setInitialPollInterval(int time)

以毫秒為單位設定初始輪詢間隔。

void setMaxPollInterval(int time)

以毫秒為單位設定初始輪詢間隔。

void setMaxTime(int time)

設定以毫秒為單位,持續嘗試要求的最長時間。

void setOpTimeout(int time)

以毫秒為單位設定作業逾時。

公用建構函式

HttpHelper

public HttpHelper ()

公用方法

buildParameters

public String buildParameters (MultiMap<String, String> paramMap)

建構已編碼的參數字串。

參數
paramMap MultiMap:網址參數

傳回
String 編碼參數字串

buildUrl

public String buildUrl (String baseUrl, 
                MultiMap<String, String> paramMap)

建構完整的已編碼網址要求字串。

參數
baseUrl String:基準網址

paramMap MultiMap:網址參數

傳回
String 建構的網址

createConnection

public HttpURLConnection createConnection (URL url, 
                String method, 
                String contentType)

建立前往指定網址的連結。

參數
url URL:要連線的 URL

method String:HTTP 要求方法。例如 GET 或 POST。

contentType String:內容類型。例如「text/html」。

傳回
HttpURLConnection HttpURLConnection

擲回
IOException

createJsonConnection

public HttpURLConnection createJsonConnection (URL url, 
                String method)

建立與指定網址的連線,用於傳遞 JSON 資料。

參數
url URL:要連線的 URL

method String:HTTP 要求方法。例如 GET 或 POST。

傳回
HttpURLConnection HttpURLConnection

擲回
IOException

createXmlConnection

public HttpURLConnection createXmlConnection (URL url, 
                String method)

建立連線至指定網址,以傳遞 XML 資料。

參數
url URL:要連線的 URL

method String:HTTP 要求方法。例如 GET 或 POST。

傳回
HttpURLConnection HttpURLConnection

擲回
IOException

doGet

public String doGet (String url)

針對指定網址執行 GET HTTP 要求方法,並以 String 形式傳回。

由於系統會將遠端內容載入記憶體,因此這個方法只適用於相對較小的資料大小。

參考資料:

參數
url String:網址

傳回
String String 遠端內容

擲回
IHttpHelper.DataSizeException
IOException

doGet

public void doGet (String url, 
                OutputStream outputStream)

針對指定網址執行 GET HTTP 要求方法,並將結果串流至 OutputStream

參數
url String:網址

outputStream OutputStream:回應資料串流

擲回
IOException

doGetIgnore

public void doGetIgnore (String url)

針對指定網址執行 GET,並忽略結果 (使用指定網址參數)。

參數
url String:網址

擲回
IOException

doGetIgnoreWithRetry

public void doGetIgnoreWithRetry (String url)

如果失敗,會執行 {doGetIgnore(String) 重試。

參數
url String:網址

擲回
IOException

doGetWithRetry

public String doGetWithRetry (String url)

如果失敗,會執行 {doGet(String) 重試。

參數
url String:網址

傳回
String String 遠端內容

擲回
IHttpHelper.DataSizeException
IOException

doPostWithRetry

public String doPostWithRetry (String url, 
                String postData)

針對指定網址執行 POST HTTP 要求方法,並以 String 形式傳回,失敗時會重試。

由於系統會將遠端內容載入記憶體,因此這個方法只適用於相對較小的資料大小。

參數
url String:網址

postData String:連線開啟後要發布的資料

傳回
String String 遠端內容

擲回
IHttpHelper.DataSizeException
IOException

doPostWithRetry

public String doPostWithRetry (String url, 
                String postData, 
                String contentType)

針對指定網址執行 POST HTTP 要求方法,並以 String 形式傳回,失敗時會重試。

由於系統會將遠端內容載入記憶體,因此這個方法只適用於相對較小的資料大小。

參數
url String:網址

postData String:連線開啟後要發布的資料

contentType String:內容類型。例如「text/html」。

傳回
String String 遠端內容

擲回
IHttpHelper.DataSizeException
IOException

getInitialPollInterval

public int getInitialPollInterval ()

以毫秒為單位取得初始輪詢間隔。

傳回
int

getMaxPollInterval

public int getMaxPollInterval ()

以毫秒為單位取得輪詢間隔上限。

傳回
int

getMaxTime

public int getMaxTime ()

以毫秒為單位,取得要求重試次數上限。

傳回
int

getOpTimeout

public int getOpTimeout ()

以毫秒為單位取得作業逾時時間。

傳回
int

getRunUtil

public IRunUtil getRunUtil ()

取得要使用的 IRunUtil。公開,以便單元測試可以模擬。

傳回
IRunUtil

setInitialPollInterval

public void setInitialPollInterval (int time)

以毫秒為單位設定初始輪詢間隔。

參數
time int

setMaxPollInterval

public void setMaxPollInterval (int time)

以毫秒為單位設定初始輪詢間隔。

參數
time int

setMaxTime

public void setMaxTime (int time)

設定以毫秒為單位的要求重試時間上限。

參數
time int

setOpTimeout

public void setOpTimeout (int time)

以毫秒為單位設定作業逾時時間。

參數
time int