A wrapper class around HttpURLConnection that guarantees a specific timeout.
Example
GuaranteedTimeoutConnection gtc = new GuaranteedTimeoutConnection(30000, false, new Handler(), "Download 123");
InputStreamCallback inputStreamCallback = new InputStreamCallback()
{
@Override
public void getInputStream(InputStream inputStream, Exception exception, String TAG)
{
}
};
gtc.getInputStream(inputStreamCallback, new URL("http://www.google.com", false);
The constructor takes four params:
- Timeout; an integer values in milliseconds.
- useSSL; a boolean that will either utilize HttpURLConnection or HttpsURLConnection respectively.
- uiHandler; Pass in a handler to the ui thread.
- tag; Pass a tag for identification that is returned in the callback.
There's 4 public methods:
1) A getter to retrieve the HttpURLConnection reference.
2) A getter to retrieve the HttpsURLConnection reference.
3) A convenient method to get an inputStream on a supplied URL. Provide an InputStreamCallback and the URL to connect to. If an input stream is not returned within the specified timeut a standard SocketException/IOException gets returned in the callback.
4) A convenient method to open the connection. Supply OpenConnectionCallback and the URL to connect to. If a connection is not opened within the specified timeout a standard SocketException/IOException gets returned in the callback.
After the callback is hit in the openConnection method, you'll likely want a reference to the URLConnection which can be retrieved using one of the available public getters.
Copyright 2012 Noah Seidman
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.