Android Open Source - Android-Fast-ImageLoader D Http Client






From Project

Back to project page Android-Fast-ImageLoader.

License

The source code is released under:

Apache License

If you think the Android project Android-Fast-ImageLoader 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 com.sunny.net;
//from  w  ww .j a  v  a  2 s  .c  o m
import android.content.Context;
import android.os.Build;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class DHttpClient {

  public void downloadInFile(String urlString, File file, Context context)
      throws IOException {
    /**
     * Workaround for bug pre-Froyo, see here for more info: http://android
     * -developers.blogspot.com/2011/09/androids-http-clients.html
     */
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
      System.setProperty("http.keepAlive", "false");
    }
    HttpURLConnection urlConnection = null;
    FileOutputStream out = null;
    File cacheFile = file;
    if (!cacheFile.exists()) {
      cacheFile.getParentFile().mkdirs();
    }
    URL url;
    InputStream is = null;
    try {
      url = new URL(urlString);
      java.net.Proxy proxy = ProxyUtil.getProxy(context);
      urlConnection = (proxy != null ? (HttpURLConnection) url
          .openConnection(proxy) : (HttpURLConnection) url
          .openConnection());
      urlConnection.setConnectTimeout(20000);
      urlConnection.setReadTimeout(20000);
      is = urlConnection.getInputStream();
      out = new FileOutputStream(cacheFile);
      byte[] buffer = new byte[8 * 1024];
      int b = -1;
      while ((b = is.read(buffer)) != -1) {
        out.write(buffer, 0, b);
      }
      if (cacheFile != null
          && cacheFile.length() < urlConnection.getContentLength()) {
        if (cacheFile.exists()) {
          cacheFile.delete();
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
      throw e;
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (out != null) {
        try {
          out.flush();
          out.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }

    cacheFile.setLastModified(System.currentTimeMillis());
  }

  public InputStream downloadInMemory(String urlString, Context context)
      throws IOException {
    /**
     * Workaround for bug pre-Froyo, see here for more info: http://android
     * -developers.blogspot.com/2011/09/androids-http-clients.html
     */
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
      System.setProperty("http.keepAlive", "false");
    }
    HttpURLConnection urlConnection = null;
    URL url;
    InputStream is = null;
    try {
      url = new URL(urlString);
      java.net.Proxy proxy = ProxyUtil.getProxy(context);
      urlConnection = (proxy != null ? (HttpURLConnection) url
          .openConnection(proxy) : (HttpURLConnection) url
          .openConnection());
      urlConnection.setConnectTimeout(20000);
      urlConnection.setReadTimeout(20000);
      is = urlConnection.getInputStream();
    } catch (IOException e) {
      e.printStackTrace();
      throw e;
    }
    return is;
  }

}




Java Source Code List

com.sunny.cache.BaseLoadListener.java
com.sunny.cache.CacheWorker.java
com.sunny.cache.DiskLruCache.java
com.sunny.cache.FileLastModifSort.java
com.sunny.cache.GIFDecoder.java
com.sunny.cache.IDownloadHandler.java
com.sunny.cache.MemoryCache.java
com.sunny.cache.OnSetImageListener.java
com.sunny.cache.RecyclingBitmapDrawable.java
com.sunny.cache.ThumbnailLoader.java
com.sunny.net.DHttpClient.java
com.sunny.net.ProxyUtil.java
com.sunny.threadpool.AbstractCommand.java
com.sunny.threadpool.CommandFactory.java
com.sunny.threadpool.DThreadPool.java
com.sunny.threadpool.IDThreadPool.java
com.sunny.threadpool.IPriorityTask.java
com.sunny.threadpool.ITaskHandler.java
com.sunny.threadpool.PriorityTask.java
com.sunny.threadpool.PriorityThreadFactory.java
com.sunny.threadpool.TaskPriority.java
com.sunny.util.ImageUtil.java
com.sunny.util.MD5Util.java
com.sunny.util.Utils.java