Android Open Source - FrameLite Abstract Http Request






From Project

Back to project page FrameLite.

License

The source code is released under:

GNU General Public License

If you think the Android project FrameLite 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.miku.framelite.api.http;
//from ww w. j  av a  2  s.  co m
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.params.ConnManagerParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;



import com.miku.framelite.api.BaseRequest;
import com.miku.framelite.api.RetResult;
import com.miku.framelite.utils.Log;

/**
 * 
 * Http?????
 * 
 * @author xr.lee
 * 
 * @param <T>
 *            ???????????
 * @param <V>
 *            ???????????????V???????????????T
 */
public abstract class AbstractHttpRequest<T, V> extends BaseRequest<T, V> {
  private static final String TAG = AbstractHttpRequest.class.getSimpleName();

  private static final int DEFAULT_TIMEOUT = 8 * 1000;

  private HashMap<String, String> headers = new HashMap<String, String>();

  /**
   * ?????{@link HttpType#POST}?{@link HttpType#GET}
   * 
   * @return
   */
  protected abstract HttpType getHttpType();

  /**
   * 
   * @return ???url
   */
  protected abstract String getUrl();

  /**
   * ???????HttpEntity
   * 
   * @return
   */
  protected abstract HttpEntity getBody();

  /**
   * ??????rawHttpEntity???????????V,???onParse?????
   * 
   * @param rawHttpEntity
   * @return ????????V
   * @throws Exception
   */
  protected abstract V handleRawContent(HttpEntity rawHttpEntity) throws Exception;

  /**
   * ????
   * 
   * @return ???????????
   */
  protected int getTimeOut() {
    return DEFAULT_TIMEOUT;
  }

  @Override
  public V doInBackground(RetResult<T> result) {
    isCancelled=false;
    HttpClient httpClient = createHttpClient();
    HttpRequestBase request = null;
    if (getHttpType() == HttpType.GET) {
      request = new HttpGet(getUrl());
    } else {
      HttpPost rq = new HttpPost(getUrl());
      if (getBody() != null) {
        rq.setEntity(getBody());
      }
      request = rq;
    }
    addHeadersToRequest(request);
    try {
      HttpResponse response = httpClient.execute(request);
      if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        return handleRawContent(response.getEntity());
      } else {
        result.setRetCode(response.getStatusLine().getStatusCode());
        result.setMsg(response.getStatusLine().toString());
        Log.e(TAG, response.getStatusLine().toString());
      }
    } catch (SocketTimeoutException e) {
      e.printStackTrace();
      result.setRetCode(RetResult.RET_ERROR_TIMEOUT);
      result.setMsg("Socket Timeout:" + e.getMessage());
    } catch (ConnectTimeoutException e) {
      e.printStackTrace();
      result.setRetCode(RetResult.RET_ERROR_TIMEOUT);
      result.setMsg("Connect Timeout:" + e.getMessage());
    } catch (MalformedURLException e) {
      e.printStackTrace();
      result.setRetCode(RetResult.RET_ERROR_UNKNOWN);
      result.setMsg("Bad Url:" + getUrl() + " MalformedURLException:" + e.getMessage());
    } catch (IOException e) {
      e.printStackTrace();
      result.setRetCode(RetResult.RET_ERROR_UNKNOWN);
      result.setMsg("IOException:" + e.getMessage());
    } catch (Exception e) {
      e.printStackTrace();
      result.setRetCode(RetResult.RET_ERROR_UNKNOWN);
      result.setMsg("Exception:" + e.getMessage());
    }

    return null;
  }

  private void addHeadersToRequest(HttpRequestBase request) {
    for (String name : headers.keySet()) {
      request.addHeader(name, headers.get(name));
    }
  }

  protected void initializeHeaders() {
    addHeader(HTTP.CONN_CLOSE, "close");
  }

  /**
   * ?????
   * 
   * @param name
   *            ??????
   * @param value
   *            ???
   */
  protected final void addHeader(String name, String value) {
    headers.put(name, value);
  }

  private HttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpConnectionParams.setTcpNoDelay(params, true);
    ConnManagerParams.setTimeout(params, getTimeOut());
    HttpConnectionParams.setConnectionTimeout(params, getTimeOut());
    HttpConnectionParams.setSoTimeout(params, getTimeOut());
    return new DefaultHttpClient(params);
  }
  
  

}




Java Source Code List

com.miku.framelite.FrameApplication.java
com.miku.framelite.FrameBaseActivity.java
com.miku.framelite.FrameOrmBaseActivity.java
com.miku.framelite.adapter.FrameBaseAdapter.java
com.miku.framelite.annotations.ViewInject.java
com.miku.framelite.api.BaseRequest.java
com.miku.framelite.api.IRequest.java
com.miku.framelite.api.RetResult.java
com.miku.framelite.api.core.Executor.java
com.miku.framelite.api.database.AbstractDatabaseRequest.java
com.miku.framelite.api.database.AbstractOrmDatabaseRequest.java
com.miku.framelite.api.database.DatabaseQueryRequest.java
com.miku.framelite.api.database.DatabaseType.java
com.miku.framelite.api.http.AbstractHttpRequest.java
com.miku.framelite.api.http.HttpStringGetRequest.java
com.miku.framelite.api.http.HttpStringPostRequest.java
com.miku.framelite.api.http.HttpType.java
com.miku.framelite.api.webservice.AbstractWebServiceRequest.java
com.miku.framelite.api.webservice.WebServiceConnectionSE.java
com.miku.framelite.api.webservice.WebServiceHttpTransportSE.java
com.miku.framelite.api.webservice.WebServiceJsonRequest.java
com.miku.framelite.api.webservice.WebServiceStringRequest.java
com.miku.framelite.httpx.IDownloadHandler.java
com.miku.framelite.httpx.IHttpX.java
com.miku.framelite.httpx.core.DownloadHandler.java
com.miku.framelite.httpx.core.HttpX.java
com.miku.framelite.services.CrashHandler.java
com.miku.framelite.utils.BitmapUtils.java
com.miku.framelite.utils.DateUtils.java
com.miku.framelite.utils.DimensionUtils.java
com.miku.framelite.utils.EncryptionUtils.java
com.miku.framelite.utils.HttpUtils.java
com.miku.framelite.utils.Log.java
com.miku.framelite.utils.TelePhoneUtils.java
com.miku.framelite.utils.ViewUtils.java