aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaineel <jaineelm@google.com>2019-08-02 16:58:45 -0700
committerJaineel Mehta <jaineelm@google.com>2019-08-06 21:33:36 +0000
commitf9ac079d5db6e70c2c0ead8cdb15cc1a0c8c43c2 (patch)
tree3c9e13f45e9c8f9f1c9a3b298c032b115ed5f3d5
parent5e3432e5bd461dba0c7fe361565cf9e92023dc4a (diff)
downloadsl4a-f9ac079d5db6e70c2c0ead8cdb15cc1a0c8c43c2.tar.gz
Adding timeout support for httpPing
optional param timeout which user can either pass or it defaults to 50 secs, which is less than sl4a rpc timeout of 60 secs Test: Yes, locally Bug: 115916707 Change-Id: I99c8a3944821a7e47f79f435cd4b6828a2533692
-rw-r--r--Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java18
1 files changed, 13 insertions, 5 deletions
diff --git a/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java b/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
index 6f173b52..0d3d8875 100644
--- a/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/wifi/HttpFacade.java
@@ -96,13 +96,19 @@ public class HttpFacade extends RpcReceiver {
* Send an http request and get the response.
*
* @param url The url to send request to.
+ * @param timeout Time to load the page
* @return The HttpURLConnection object.
*/
- private HttpURLConnection httpRequest(String url) throws IOException {
+ private HttpURLConnection httpRequest(String url, Integer timeout) throws IOException {
+ if (timeout == null) {
+ timeout = 50000;
+ }
URL targetURL = new URL(url);
HttpURLConnection urlConnection;
try {
urlConnection = (HttpURLConnection) targetURL.openConnection();
+ urlConnection.setConnectTimeout(9000);
+ urlConnection.setReadTimeout(timeout);
urlConnection.connect();
int respCode = urlConnection.getResponseCode();
String respMsg = urlConnection.getResponseMessage();
@@ -132,7 +138,7 @@ public class HttpFacade extends RpcReceiver {
public void httpDownloadFile(@RpcParameter(name = "url") String url,
@RpcParameter(name="outPath") @RpcOptional String outPath) throws IOException {
// Create the input stream
- HttpURLConnection urlConnection = httpRequest(url);
+ HttpURLConnection urlConnection = httpRequest(url, null);
// Parse destination path and create the output stream. The function assumes that the path
// is specified relative to the system default Download dir.
File outFile = FileUtils.getExternalDownload();
@@ -179,10 +185,12 @@ public class HttpFacade extends RpcReceiver {
}
@Rpc(description = "Make an http request and return the response message.")
- public HttpURLConnection httpPing(@RpcParameter(name = "url") String url) throws IOException {
+ public HttpURLConnection httpPing(
+ @RpcParameter(name = "url") String url,
+ @RpcParameter(name = "timeout") @RpcOptional Integer timeout) throws IOException {
try {
HttpURLConnection urlConnection = null;
- urlConnection = httpRequest(url);
+ urlConnection = httpRequest(url, timeout);
urlConnection.disconnect();
return urlConnection;
} catch (UnknownHostException e) {
@@ -192,7 +200,7 @@ public class HttpFacade extends RpcReceiver {
@Rpc(description = "Make an http request and return the response content as a string.")
public String httpRequestString(@RpcParameter(name = "url") String url) throws IOException {
- HttpURLConnection urlConnection = httpRequest(url);
+ HttpURLConnection urlConnection = httpRequest(url, null);
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
String result = inputStreamToString(in);
Log.d("Fetched: " + result);