Android Open Source - Boxee-Thumb-Remote Reusable Http Client Blocking






From Project

Back to project page Boxee-Thumb-Remote.

License

The source code is released under:

Apache License

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

/* The following code was written by Menny Even Danan
 * and is released under the APACHE 2.0 license
 * /*from w ww . j  a v  a2  s .  c om*/
 * http://www.apache.org/licenses/LICENSE-2.0
 */
package net.evendanan.android.thumbremote.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;

import android.text.TextUtils;
import android.util.Log;

/**
 * Wraps Android's HttpClient/HttpGet objects
 */
class ReusableHttpClientBlocking implements HttpBlocking {
  private static final String TAG = "ReusableHttpClientBlocking";
  
  private final DefaultHttpClient mHttpClient;
  private final HttpGet mRequest;
  
  ReusableHttpClientBlocking(int timeout, String user, String password) {
    Log.d(TAG, "Creating a new HTTP client");
    mHttpClient = new DefaultHttpClient();
    if (!TextUtils.isEmpty(password)) {
      CredentialsProvider credProvider = new BasicCredentialsProvider();
        credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), 
            new UsernamePasswordCredentials(user, password));
        mHttpClient.setCredentialsProvider(credProvider);
    }
    mRequest = new HttpGet();
    mRequest.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(timeout));
    mRequest.getParams().setParameter(CoreConnectionPNames.SO_LINGER, new Integer(timeout/2));
    mRequest.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeout));
  }

  /**
   * Perform the blocking fetch.
   * @throws IOException 
   * @throws URISyntaxException 
   */
  public synchronized Response fetch(String url) throws IOException, URISyntaxException {
    if (url == null)
      return new Response(false, 404, null);

    URI uri = new URI(url);
    mRequest.setURI(uri);
    Log.d(TAG, "Fetching " + url);
    
      HttpResponse httpResponse = mHttpClient.execute(mRequest);
        int responseCode = httpResponse.getStatusLine().getStatusCode();
        String response = "";
        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

          InputStream instream = entity.getContent();
            response = convertStreamToString(instream);
        }

        boolean success = responseCode >= 200 && responseCode < 300;
        if (!success)
        {
          Log.w(TAG, "Got error response code "+responseCode);
          return new Response(false, responseCode, httpResponse.getStatusLine().getReasonPhrase());
        }
        
        return new Response(true, responseCode, response);
  }
  
  @Override
  public synchronized void close() {
    mHttpClient.getConnectionManager().shutdown();
  }

    private static String convertStreamToString(InputStream is) {

        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        
        String NL = System.getProperty("line.separator");
        String line = null;
        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append(NL);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }
}




Java Source Code List

.HttpClientBlocking.java
com.example.android.actionbarcompat.ActionBarHelperBase.java
com.example.android.actionbarcompat.ActionBarHelperCompat.java
com.example.android.actionbarcompat.ActionBarHelperHoneycomb.java
com.example.android.actionbarcompat.ActionBarHelperICS.java
com.example.android.actionbarcompat.SimpleMenuItem.java
com.example.android.actionbarcompat.SimpleMenu.java
iharder.base64.Base64.java
net.evendanan.android.thumbremote.MediaStateListener.java
net.evendanan.android.thumbremote.RemoteApplication.java
net.evendanan.android.thumbremote.ServerAddress.java
net.evendanan.android.thumbremote.ServerConnectionListener.java
net.evendanan.android.thumbremote.ServerConnector.java
net.evendanan.android.thumbremote.ServerRemote.java
net.evendanan.android.thumbremote.ServerStatePoller.java
net.evendanan.android.thumbremote.ServerStateUrlsProvider.java
net.evendanan.android.thumbremote.ServerState.java
net.evendanan.android.thumbremote.Settings.java
net.evendanan.android.thumbremote.ShakeListener.java
net.evendanan.android.thumbremote.UiView.java
net.evendanan.android.thumbremote.boxee.BoxeeConnector.java
net.evendanan.android.thumbremote.boxee.BoxeeDiscovererThread.java
net.evendanan.android.thumbremote.network.HttpBlocking.java
net.evendanan.android.thumbremote.network.HttpRequest.java
net.evendanan.android.thumbremote.network.Response.java
net.evendanan.android.thumbremote.network.ReusableHttpClientBlocking.java
net.evendanan.android.thumbremote.service.DoServerRemoteAction.java
net.evendanan.android.thumbremote.service.ServerRemoteService.java
net.evendanan.android.thumbremote.service.State.java
net.evendanan.android.thumbremote.ui.FixedViewFlipper.java
net.evendanan.android.thumbremote.ui.FragmentAlertDialogSupport.java
net.evendanan.android.thumbremote.ui.HelpUiActivity.java
net.evendanan.android.thumbremote.ui.RemoteUiActivity.java
net.evendanan.android.thumbremote.ui.SettingsActivity.java