Android Open Source - visiting-card-android Async H






From Project

Back to project page visiting-card-android.

License

The source code is released under:

GNU General Public License

If you think the Android project visiting-card-android 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.matrix.asynchttplibrary;
/*from   ww  w. j  av  a 2  s .  c  o m*/
import java.io.UnsupportedEncodingException;
import java.security.KeyStore;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.entity.StringEntity;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.matrix.asynchttplibrary.logger.ALogger;
import com.matrix.asynchttplibrary.model.CallProperties;
import com.matrix.asynchttplibrary.request.AsyncRequestHeader;
import com.matrix.asynchttplibrary.request.AsyncRequestParam;
import com.matrix.asynchttplibrary.security.CustomSSLSocketFactory;
import com.matrix.asynchttplibrary.util.AsyncUtil;

/**
 * 
 * @author Yajnesh T
 * 
 * @version v1.0
 * 
 * @description
 * 
 *              AsyncH is a simple HTTP/S library based on
 *              android-async-http-1.4.4.jar <br>
 *              In addition to the features provided by android-async-http,
 *              "AsyncH" keeps your code clean and organized with the following
 *              features <br>
 * <br>
 *              Features:<br>
 * 
 *              1) Generic binding of Request/Response/Header Models <br>
 *              2) Annotation support to modify parameters (prefix, postfix and
 *              override) <br>
 *              3) Automatically pick up protocol/method and baseurl from
 *              property file. The method "communicate" automatically picks us
 *              suitable protocol and method <br>
 *              4) String/JSON/JSONArray Parsers to handle the response <br>
 * 
 */
public class AsyncH extends AsyncHttpClient {

  private Context context = null;

  public AsyncH() {
    super();
  }

  public AsyncH(boolean fixNoHttpResponseException, int httpPort,
      int httpsPort) {
    super(fixNoHttpResponseException, httpPort, httpsPort);
  }

  public AsyncH(int httpPort, int httpsPort) {
    super(httpPort, httpsPort);
  }

  public AsyncH(int httpPort) {
    super(httpPort);
  }

  public AsyncH(SchemeRegistry schemeRegistry) {
    super(schemeRegistry);
  }

  /**
   * Generic method to make a HTTP/S request
   * 
   * @param callProperties
   *            : consists protocol(Currently only REST), method (Currently
   *            only GET and POST) and baseURL
   * @param header
   *            : header values to be bound
   * @param param
   *            : parameters to be bound
   * @param handler
   *            : On success/failure callback
   */
  public void communicate(CallProperties callProperties,
      AsyncRequestHeader header, AsyncRequestParam param,
      AsyncHttpResponseHandler handler) {
    if (callProperties == null) {
      ALogger.e("callProperties is null");
      return;
    }

    if (callProperties.protocol.equalsIgnoreCase("REST")) {
      generateRESTRequest(callProperties, header, param, handler);
    } else {
      ALogger.e("Not implemented " + callProperties.protocol);
    }

  }

  private void generateRESTRequest(CallProperties callProperties,
      AsyncRequestHeader header, AsyncRequestParam param,
      AsyncHttpResponseHandler handler) {

    if (callProperties.method.equalsIgnoreCase("GET")) {
      generateGetRequest(callProperties.baseURL, header, param, handler);
    } else if (callProperties.method.equalsIgnoreCase("POST")) {
      generatePostRequest(callProperties.baseURL, header, param, handler);
    } else if (callProperties.method.equalsIgnoreCase("DELETE")) {
      generateDeleteRequest(callProperties.baseURL, header, param,
          handler);
    } else {
      ALogger.e("Not implemented " + callProperties.method);
    }
  }

  private void generateDeleteRequest(String baseURL,
      AsyncRequestHeader header, AsyncRequestParam param,
      AsyncHttpResponseHandler handler) {

    if (header != null) {
      bindHeaders(header);
    }

    if (baseURL == null) {
      ALogger.e("baseUrl is null");
      handler.onFailure(0, null, "baseUrl is null".getBytes(), null);
      return;
    }
    if (handler == null) {
      ALogger.e("handler is null");
      return;
    }
    delete(baseURL, handler);

  }

  private void generatePostRequest(String baseURL, AsyncRequestHeader header,
      AsyncRequestParam param, AsyncHttpResponseHandler handler) {
    if (param.getRequestType() == AsyncRequestParam.Type.DEFAULT) {
      generatePostRequestDefault(baseURL, header, param, handler);
    } else if (param.getRequestType() == AsyncRequestParam.Type.JSON) {
      generatePostRequestJSON(baseURL, header, param, handler);

    }
  }

  private void generatePostRequestJSON(String baseURL,
      AsyncRequestHeader header, AsyncRequestParam param,
      AsyncHttpResponseHandler handler) {

    if (context == null) {
      ALogger.e("Context is null, POST request with JSON params require context, AsyncH asyncHRequest = new AsyncH(); asyncHRequest.setContext(getApplicationContext());");
      return;
    }

    if (header != null) {
      bindHeaders(header);
    }

    JSONObject jsonParams = null;
    if (param != null) {
      jsonParams = (JSONObject) AsyncUtil.processParams(param, false);

    }

    if (baseURL == null) {
      ALogger.e("baseUrl is null");
      handler.onFailure(0, null, "baseUrl is null".getBytes(), null);
      return;
    }
    if (handler == null) {
      ALogger.e("handler is null");
      return;
    }

    StringEntity entity = null;
    try {
      entity = new StringEntity(jsonParams.toString());
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    if (entity == null) {
      ALogger.e("Unable to create entity from : " + jsonParams.toString());
      return;
    }

    post(context, baseURL, entity, "application/json", handler);

  }

  private void generatePostRequestDefault(String baseUrl,
      AsyncRequestHeader header, AsyncRequestParam param,
      AsyncHttpResponseHandler handler) {

    RequestParams params = null;

    if (header != null) {
      bindHeaders(header);
    }
    if (param != null) {
      params = (RequestParams) AsyncUtil.processParams(param, false);

    }

    if (baseUrl == null) {
      ALogger.e("baseUrl is null");
      handler.onFailure(0, null, "baseUrl is null".getBytes(), null);
      return;
    }
    if (handler == null) {
      ALogger.e("handler is null");
      return;
    }

    post(baseUrl, params, handler);

  }

  public void generatePostRequestTemperoryMethod(String baseUrl,
      AsyncRequestHeader header, RequestParams param,
      AsyncHttpResponseHandler handler) {

    if (header != null) {
      bindHeaders(header);
    }

    if (baseUrl == null) {
      ALogger.e("baseUrl is null");
      handler.onFailure(0, null, "baseUrl is null".getBytes(), null);
      return;
    }
    if (handler == null) {
      ALogger.e("handler is null");
      return;
    }

    post(baseUrl, param, handler);

  }

  private void generateGetRequest(String baseUrl, AsyncRequestHeader header,
      AsyncRequestParam param, AsyncHttpResponseHandler handler) {
    RequestParams params = null;

    if (header != null) {
      bindHeaders(header);
    }
    if (param != null) {
      params = (RequestParams) AsyncUtil.processParams(param, true);

    }

    if (baseUrl == null) {
      ALogger.e("baseUrl is null");
      handler.onFailure(0, null, "baseUrl is null".getBytes(), null);
      return;
    }
    if (handler == null) {
      ALogger.e("handler is null");
      return;
    }

    get(baseUrl, params, handler);

  }

  private void bindHeaders(AsyncRequestHeader header) {

    HashMap<String, String> modelMap = AsyncUtil.createHashMapFromModel(
        header, false);

    Set<Entry<String, String>> set = modelMap.entrySet();

    Iterator<Entry<String, String>> iterator = set.iterator();

    while ((iterator.hasNext())) {
      Entry<String, String> data = iterator.next();
      ALogger.v(data.getKey() + ":" + data.getValue());
      addHeader(data.getKey(), data.getValue());
    }
  }

  /**
   * Use with caution, Accepts all secure certificates.
   */
  public void enableHTTPSCertificates() {

    try {

      KeyStore trustStore = KeyStore.getInstance(KeyStore
          .getDefaultType());
      trustStore.load(null, null);
      CustomSSLSocketFactory sf = new CustomSSLSocketFactory(trustStore);
      sf.setHostnameVerifier(CustomSSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      setSSLSocketFactory(sf);
    } catch (Exception e) {
      Log.e(getClass().getSimpleName(),
          "Unable to set HTTPS certificate  \n" + e.getMessage());
    }
  }

  public void setContext(Context context) {
    this.context = context;
  }

}




Java Source Code List

android.UnusedStub.java
com.loopj.android.http.AsyncHttpClient.java
com.loopj.android.http.AsyncHttpRequest.java
com.loopj.android.http.AsyncHttpResponseHandler.java
com.loopj.android.http.Base64DataException.java
com.loopj.android.http.Base64OutputStream.java
com.loopj.android.http.Base64.java
com.loopj.android.http.BaseJsonHttpResponseHandler.java
com.loopj.android.http.BinaryHttpResponseHandler.java
com.loopj.android.http.DataAsyncHttpResponseHandler.java
com.loopj.android.http.FileAsyncHttpResponseHandler.java
com.loopj.android.http.JsonHttpResponseHandler.java
com.loopj.android.http.JsonStreamerEntity.java
com.loopj.android.http.MyRedirectHandler.java
com.loopj.android.http.MySSLSocketFactory.java
com.loopj.android.http.PersistentCookieStore.java
com.loopj.android.http.PreemtiveAuthorizationHttpRequestInterceptor.java
com.loopj.android.http.RangeFileAsyncHttpResponseHandler.java
com.loopj.android.http.RequestHandle.java
com.loopj.android.http.RequestParams.java
com.loopj.android.http.ResponseHandlerInterface.java
com.loopj.android.http.RetryHandler.java
com.loopj.android.http.SaxAsyncHttpResponseHandler.java
com.loopj.android.http.SerializableCookie.java
com.loopj.android.http.SimpleMultipartEntity.java
com.loopj.android.http.SyncHttpClient.java
com.loopj.android.http.TextHttpResponseHandler.java
com.loopj.android.http.package-info.java
com.matrix.asynchttplibrary.AsyncH.java
com.matrix.asynchttplibrary.annotation.AsyncHAnnotation.java
com.matrix.asynchttplibrary.annotation.AsyncHIgnoreParam.java
com.matrix.asynchttplibrary.logger.ALogger.java
com.matrix.asynchttplibrary.model.CallProperties.java
com.matrix.asynchttplibrary.parser.AsyncParser.java
com.matrix.asynchttplibrary.request.AsyncRequestHeader.java
com.matrix.asynchttplibrary.request.AsyncRequestParam.java
com.matrix.asynchttplibrary.response.AsyncResponseBody.java
com.matrix.asynchttplibrary.security.CustomSSLSocketFactory.java
com.matrix.asynchttplibrary.util.AsyncUtil.java
com.matrix.visitingcard.AllVCFragment.java
com.matrix.visitingcard.CreateVCActivity.java
com.matrix.visitingcard.ListMyVCFragment.java
com.matrix.visitingcard.ListMyVCRActivity.java
com.matrix.visitingcard.ListOfVCTFragment.java
com.matrix.visitingcard.ResideActivity.java
com.matrix.visitingcard.SelectVCActivity.java
com.matrix.visitingcard.ShareVCDialogFragment.java
com.matrix.visitingcard.SignUpFormActivity.java
com.matrix.visitingcard.SplashScreenActivity.java
com.matrix.visitingcard.VCRCreateDialogFragment.java
com.matrix.visitingcard.ViewVC.java
com.matrix.visitingcard.adapter.SupportArrayAdapter.java
com.matrix.visitingcard.adapter.VCAdapter.java
com.matrix.visitingcard.adapter.VCRAdapter.java
com.matrix.visitingcard.adapter.VCTAdapter.java
com.matrix.visitingcard.constant.Constants.java
com.matrix.visitingcard.gcm.GcmBroadcastReceiver.java
com.matrix.visitingcard.gcm.GcmIntentService.java
com.matrix.visitingcard.http.AsyncHttp.java
com.matrix.visitingcard.http.ProgressJSONResponseCallBack.java
com.matrix.visitingcard.http.ProgressJsonHttpResponseHandler.java
com.matrix.visitingcard.http.UIReloadCallBack.java
com.matrix.visitingcard.http.parser.Parser.java
com.matrix.visitingcard.http.request.AcceptVCRResquest.java
com.matrix.visitingcard.http.request.ShareVCResquest.java
com.matrix.visitingcard.http.request.SocialLoginRequest.java
com.matrix.visitingcard.http.response.FriendsVC.java
com.matrix.visitingcard.http.response.MyVC.java
com.matrix.visitingcard.http.response.VCR.java
com.matrix.visitingcard.http.response.VCTResponse.java
com.matrix.visitingcard.http.response.VC.java
com.matrix.visitingcard.logger.VLogger.java
com.matrix.visitingcard.user.User.java
com.matrix.visitingcard.util.CustomImageDownaloder.java
com.matrix.visitingcard.util.FileUtil.java
com.matrix.visitingcard.util.SharedPrefs.java
com.matrix.visitingcard.util.Util.java
com.matrix.visitingcard.util.VisitingCardApp.java
com.special.ResideMenu.ResideMenuItem.java
com.special.ResideMenu.ResideMenu.java
com.special.ResideMenu.TouchDisableView.java