Android Open Source - android_sdk Network Controller






From Project

Back to project page android_sdk.

License

The source code is released under:

GNU Lesser General Public License

If you think the Android project android_sdk 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

/*
 * Copyright (C) 2013  Spunk Media Pvt Ltd (www.qubecell.com)
 *//* ww  w .  j a  va  2  s. co m*/

package com.qubecell.network;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import android.content.Context;
import android.text.TextUtils;

import com.qubecell.constants.HttpConstant;
import com.qubecell.constants.NetworkResponse;
import com.qubecell.constants.NetworkResponseCode;
import com.qubecell.constants.NetworkURL;
import com.qubecell.constants.ServerCommand;
import com.qubecell.elogger.ELogger;

/**
 * The NetworkAsyncTask class is used to perform network related operations
 * @author Eninov
 *
 */
public class NetworkController 
{
  private ELogger eLogger = null;
  private String logTag = "Network::";
  protected Context appContext = null;
  protected HttpPost httpPost = null;
  HttpURLConnection httpConn = null;
  protected boolean initFlag = false;
  private static NetworkController nwObj =  null;
  
  public boolean init()
  {
    eLogger = new ELogger();
    eLogger.setTag(logTag);
    initFlag = true;
    nwObj = getInstance();
    return initFlag;
  }

  public static NetworkController getInstance() //pass
  {
    if(nwObj == null)
      nwObj = new NetworkController();
    return nwObj;
  }
  
  /**
   * This method is used to send http post request to server.
   * @param reqJson
   * @return
   */
  public NetworkResponse httpPost(List<NameValuePair> reqNameValuePair, int requestType) 
  {
    if(eLogger == null)
    {
      init();
    }
    
    String httpUrl = getNetworkURL(requestType);
    
    NetworkResponse netResp = null;
    if(requestType != ServerCommand.FETCH_OPR_CMD && reqNameValuePair == null || TextUtils.isEmpty(httpUrl)) 
    {
      eLogger.error("httpPost() : req json null or or null url");
    }
    else
    {
      eLogger.info("httpPost() : Request String : "+ reqNameValuePair.toString());
      netResp = new NetworkResponse();
      httpPost = null;
      try
      {
        httpPost = new HttpPost(httpUrl);
      }
      catch (IllegalArgumentException e) 
      {
        eLogger.error("httpPost() : IllegalArgumentException "+e);
        netResp.netRespCode = NetworkResponseCode.INVALID_URL;
        httpPost = null;
      }
      if(httpPost != null)
      {
        BasicHttpParams basicHttpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(basicHttpParams,HttpConstant.HTTP_REQUEST_TIMEOUT);
        HttpConnectionParams.setSoTimeout(basicHttpParams, HttpConstant.SOCKET_REQUEST_TIMEOUT);
        
        try
        { 
          if(requestType != ServerCommand.FETCH_OPR_CMD)
          {
            httpPost.setEntity(new UrlEncodedFormEntity(reqNameValuePair));
          }
        } 
        catch (UnsupportedEncodingException e) 
        {
          eLogger.error("httpPost : UnsupportedEncodingException"+e);
          httpPost = null;
          netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
          return netResp;
        }

        DefaultHttpClient httpClient = new DefaultHttpClient(basicHttpParams);
        HttpResponse response = null;
        try 
        {
          eLogger.debug("httpPost : sending request to network ");
          response = httpClient.execute(httpPost);
          eLogger.debug("httpPost : response from network  : "+response);
        } 
        catch (ClientProtocolException e)
        {
          eLogger.error("httpPost : ClientProtocolException"+e);
          e.printStackTrace();
          closeHttpPost(httpPost);
          netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
          return netResp;
        } 
        catch(ConnectTimeoutException e)
        {
          netResp.netRespCode = NetworkResponseCode.NET_REQ_TIMEOUT;
          eLogger.error("httpPost : ConnectTimeoutException"+e);
          closeHttpPost(httpPost);
          return netResp;
        } 
        catch(SocketTimeoutException e)
        {
          netResp.netRespCode = NetworkResponseCode.NET_REQ_TIMEOUT;
          eLogger.error("httpPost : SocketTimeoutException : "+e);
          closeHttpPost(httpPost);
          return netResp;
        }
        catch (Exception e) 
        {
          netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
          eLogger.error("httpPost : Network Exception"+e);
          closeHttpPost(httpPost);
          return netResp;
        }

        StringBuffer resposebuf = new StringBuffer();
        StatusLine status = response.getStatusLine();
        HttpEntity entity = response.getEntity();
        if (entity != null)
        {
          InputStreamReader inputStreamReader = null;
          BufferedReader bufferedReader = null;
          try
          {
            inputStreamReader = new InputStreamReader(entity.getContent());
            bufferedReader = new BufferedReader(inputStreamReader);
            String str;
            while ((str = bufferedReader.readLine()) != null)
            {
              resposebuf.append(str);
            }
          }
          catch (OutOfMemoryError e) 
          {
            eLogger.error("httpPost : OutOfMemoryError"+e);
            e.printStackTrace();
            netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
            return netResp;
          }
          catch (IllegalStateException e) 
          {
            eLogger.error("httpPost : IOException"+e);
            e.printStackTrace();
            netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
            return netResp;
          } 
          catch (IOException e)
          {
            eLogger.error("httpPost : IOException"+e);
            e.printStackTrace();
            netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
            return netResp;
          }
          finally
          {
            closeHttpPost(httpPost);
            closeBufferedReader(bufferedReader);
            closeInputStream(inputStreamReader);
          }
        }
        if (status.getStatusCode() >= 300) 
        {
          eLogger.error("httpPost : invalid status code HTTP:" + status.getStatusCode() + " "+ status.getReasonPhrase() + "\n" + resposebuf.toString());
          netResp.netRespCode = NetworkResponseCode.NET_EXCEPTION;
          return netResp;
        } 
        else 
        {
          netResp.netRespCode = NetworkResponseCode.NET_RESP_SUCCESS;
          netResp.respStr = resposebuf.toString();
        }
      }
      else
      {
        
      }
    }
    eLogger.info("httpPost() : Response String : "+ netResp.respStr);
    return netResp;
  }
  
  /**
   * This method is used to get the network URL for making request to the network for give request type.
   * @param requestType
   * @return
   */
  private String getNetworkURL(int requestType) 
  {
    String httpUrl = null;
    switch (requestType) {
    case ServerCommand.MSISDN_CMD:
    {
      httpUrl = NetworkURL.MSISDN_URL;
    }
    break;
    case ServerCommand.FETCH_OPR_CMD:
    {
      httpUrl = NetworkURL.OPERATOR_URL;
    }
    break;
    case ServerCommand.SUBSCRIBE_CMD:
    {
      //httpUrl = NetworkURL.SUBSCRIPTION_URL;
    }
    break;
    case ServerCommand.CHECK_STATUS_CMD:
    {
      //httpUrl = NetworkURL.SUBSCRIPTION_URL;
    }
    break;
    case ServerCommand.UNSUBSCRIBE_CDM:
    {
      //httpUrl = NetworkURL.SUBSCRIPTION_URL;
    }
    break;

    default:
    {
      httpUrl = NetworkURL.API_URL;
    }
    break;
    }
    return httpUrl;
  }

  /**
   * This method is used to close all the http post request.
   * @param httpPost
   * @return
   */
  private boolean closeHttpPost(HttpPost httpPost)
  {
    boolean rslt = false;
    if(httpPost != null)
    {
      httpPost.abort();
      httpPost = null;
      rslt = true;
    }
    else
    {
       rslt = false;
    }
    return rslt;
  }
  
  /**
   * This method is used to close input streams.
   * @param inputStream
   * @return
   */
  private boolean closeInputStream(InputStreamReader inputStream)
  {
    boolean rslt = false;
    if(inputStream != null)
    {
      try 
      {
        inputStream.close();
        rslt = true;
      } catch (IOException e) 
      {
        eLogger.error("closeInputStream() : exception while closing input stream : "+e);
        e.printStackTrace();
        rslt = false;
      }
      inputStream = null;
    }
    else
    {
      rslt = false;
    }
    return rslt;
  }
  
  /**
   * This method is used to close all the buffered readers.
   * @param bufReader
   * @return
   */
  private boolean closeBufferedReader(BufferedReader bufReader)
  {
    boolean rslt = false;
    if(bufReader != null)
    {
      try 
      {
        bufReader.close();
         rslt = true;
      }
      catch (IOException e) 
      {
        e.printStackTrace();
         rslt = false;
      }
    }
    else
    {
       rslt = false;
    }
    return rslt;
  }
}




Java Source Code List

com.qubecell.beans.BaseBean.java
com.qubecell.beans.CheckStatusRespBean.java
com.qubecell.beans.EventChargeRespBean.java
com.qubecell.beans.MsisdnRespBean.java
com.qubecell.beans.OperatorDetails.java
com.qubecell.beans.OperatorsRespBean.java
com.qubecell.beans.ResponseBaseBean.java
com.qubecell.beans.SendOTPRespBean.java
com.qubecell.beans.ValidateOTPRespBean.java
com.qubecell.constants.ApplicationActivities.java
com.qubecell.constants.CheckstatusServerRespCode.java
com.qubecell.constants.ConstantStrings.java
com.qubecell.constants.EventChargeServerRespCode.java
com.qubecell.constants.HttpConstant.java
com.qubecell.constants.IntentConstant.java
com.qubecell.constants.MerchantData.java
com.qubecell.constants.MessageResponseCode.java
com.qubecell.constants.MobileOperators.java
com.qubecell.constants.MsisdnServerRespCode.java
com.qubecell.constants.NetworkResponseCode.java
com.qubecell.constants.NetworkResponse.java
com.qubecell.constants.NetworkURL.java
com.qubecell.constants.PaymentResult.java
com.qubecell.constants.ProductIds.java
com.qubecell.constants.QubecellResult.java
com.qubecell.constants.SendOTPServerRespCode.java
com.qubecell.constants.ServerCommand.java
com.qubecell.constants.ThemeConfigurationVariables.java
com.qubecell.constants.ValidateOTPServerRespCode.java
com.qubecell.constants.WidgetsTagName.java
com.qubecell.elogger.ELogger.java
com.qubecell.network.AsyncClient.java
com.qubecell.network.NetworkController.java
com.qubecell.receivers.SMSReceiver.java
com.qubecell.smsmgr.QubecellSMSManager.java
com.qubecell.ui.BaseActivity.java
com.qubecell.ui.QubecellActivity.java
com.qubecell.ui.ResultActivity.java
com.qubecell.ui.SelectOperatorActivity.java
com.qubecell.ui.ValidateOTPActivity.java
com.qubecell.utility.CommonUtility.java
com.qubecell.utility.ImageBase64.java
com.qubecell.xmlparser.XMLParser.java
com.qubecell.xmlparser.XMLfunctions.java
com.qubecelltestapp.ui.MerchantActivity.java