If you think the Android project Boxee-Thumb-Remote listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package net.evendanan.android.thumbremote.network;
/*www.java2s.com*/import android.text.TextUtils;
import android.util.Log;
/*
* Controls the live cycle of a network connection.
* Tries to keep the connection alive as long as possible, thus, reusing HTTP connections, and reduce sockets and objects creations.
*/publicclass HttpRequest {
privatestaticfinal String TAG = "HttpRequest";
// We set these for all requests.
privatestaticint msTimeout = 2000;
privatestatic String msPassword;
privatestatic String msUser;
privatestatic HttpBlocking msRequester = null;
publicsynchronizedstaticvoid setTimeout(int timeout_ms) {
close();
msTimeout = timeout_ms;
}
publicsynchronizedstaticvoid setUserPassword(String user, String password) {
close();
msUser = user;
msPassword = password;
}
publicsynchronizedstaticboolean hasCredentials()
{
return !TextUtils.isEmpty(msUser) && !TextUtils.isEmpty(msPassword);
}
publicsynchronizedstatic Response getHttpResponse(final String url)
{
if (msRequester == null)
{
msRequester = new ReusableHttpClientBlocking(msTimeout, msUser, msPassword);
}
try
{
return msRequester.fetch(url);
}
catch(Exception e)
{
Log.w(TAG, "Caught an exception while fetching url "+url+". Error: "+e.getMessage());
close();
e.printStackTrace();
returnnew Response(false, 404, "Failed to connect to server");
}
}
publicsynchronizedstaticvoid close() {
if (msRequester != null)
msRequester.close();
msRequester = null;
}
}