Android Open Source - gm-httpengine-studio G M Http Service






From Project

Back to project page gm-httpengine-studio.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project gm-httpengine-studio 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 org.gemini.httpengine.library;
/*from  w  w  w. j  a v a  2s  .c  o m*/
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

public class GMHttpService implements GMHttpMethodExecutor{

  public static final String TAG = GMHttpService.class.getSimpleName();
  public static final String VERSION = Config.VERSION_NAME;
    private static final int MAX_THREAD_NUM = 4;

    private static GMHttpService sInstance;

  /***
   * per thread has a {@link GMHttpEngine}
   */
  private final ThreadLocal<GMHttpEngine> sHttpEnginePool = new ThreadLocal<GMHttpEngine>();

  private final Executor mService;

  private GMHttpService() {
        mService = Executors.newFixedThreadPool(MAX_THREAD_NUM);
  }

  /***
   * Single Instance
   * 
   * @return
   */
  public static synchronized GMHttpService getInstance() {
    if (sInstance == null) {
      makeInstance();
    }
    return sInstance;
  }

  private static synchronized void makeInstance() {
        sInstance = new GMHttpService();
  }


  private class HttpRunnable implements Runnable {

    private GMHttpRequest mHttpRequest;
    private GMHttpResponse mHttpResponse;

    public HttpRunnable(GMHttpRequest httpRequest) {
      mHttpRequest = httpRequest;
    }

    @Override
    public void run() {
      if (mHttpRequest.isCancel()) {
        return;
      }
      GMHttpEngine httpEngine = sHttpEnginePool.get();
      if (httpEngine == null) {
        httpEngine = new GMHttpEngine();
        sHttpEnginePool.set(httpEngine);
      }

      // execute request and get response
            mHttpResponse = httpEngine.openUrl(mHttpRequest);
      this.updateResponse();
    }

    private void updateResponse() {
      if (mHttpRequest.isCancel()) {
        return;
      }
      OnResponseListener l = mHttpRequest.getOnResponseListener();
      if (null != l) {
        l.onResponse(mHttpResponse, mHttpRequest);
      }
    }
  }

  /***
   * Execute http request asynchronous and get the response
   * 
   * @param httpRequest
   *            the http request object
   */
    @Override
  public void executeHttpMethod(GMHttpRequest httpRequest) {
    Runnable runnable = new HttpRunnable(httpRequest);
    mService.execute(runnable);
  }

  /***
   * Cancel request
   * 
   * @param httpRequest
   *            the request to cancel
   */
    @Override
  public void cancelRequest(GMHttpRequest httpRequest) {
    //@TODO: to be completed;
        httpRequest.cancel();
  }

}




Java Source Code List

org.gemini.httpengine.examples.LoginAPI.java
org.gemini.httpengine.examples.MainActivity.java
org.gemini.httpengine.library.Config.java
org.gemini.httpengine.library.FormUrlEncodedParser.java
org.gemini.httpengine.library.GMHttpEngine.java
org.gemini.httpengine.library.GMHttpException.java
org.gemini.httpengine.library.GMHttpLoader.java
org.gemini.httpengine.library.GMHttpMethodExecutor.java
org.gemini.httpengine.library.GMHttpParameters.java
org.gemini.httpengine.library.GMHttpRequest.java
org.gemini.httpengine.library.GMHttpResponse.java
org.gemini.httpengine.library.GMHttpResult.java
org.gemini.httpengine.library.GMHttpService.java
org.gemini.httpengine.library.GMModelParser.java
org.gemini.httpengine.library.GMTrustManager.java
org.gemini.httpengine.library.HttpMethod.java
org.gemini.httpengine.library.HttpParameter.java
org.gemini.httpengine.library.HttpRequestParser.java
org.gemini.httpengine.library.HttpResponseParser.java
org.gemini.httpengine.library.LOG.java
org.gemini.httpengine.library.MultiPartParser.java
org.gemini.httpengine.library.MultipartEntity.java
org.gemini.httpengine.library.OnProgressUpdateListener.java
org.gemini.httpengine.library.OnResponseListener.java