Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import org.apache.http.HttpVersion;

import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.params.ConnManagerPNames;
import org.apache.http.conn.params.ConnPerRouteBean;

import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.SingleClientConnManager;
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 android.content.Context;

import android.util.Log;

public class Main {
    private static final String TAG = "CommonUtils";

    public static DefaultHttpClient setHttpPostProxyParams(Context context, int connTimeout) {

        Log.d(TAG, "ctx, connTimeout -- called");

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        //schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

        HttpParams params = new BasicHttpParams();

        /*Log.d(TAG, "----Add Proxy---");
        HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
          Constants.PROXY_PORT);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/

        /*if ((getWifiName(context).trim()).equalsIgnoreCase("secure-impact")) {
           Log.d(TAG, "----Add Proxy---");
           HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
             Constants.PROXY_PORT);
           params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }*/

        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        HttpConnectionParams.setConnectionTimeout(params, connTimeout);
        HttpConnectionParams.setSoTimeout(params, connTimeout);

        ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

        DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params);

        return mHttpClient;
    }

    public static DefaultHttpClient setHttpPostProxyParams(Context context) {

        Log.d(TAG, "ctx -- called ");

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        //schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

        HttpParams params = new BasicHttpParams();

        /*Log.d(TAG, "----Add Proxy---");
        HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
          Constants.PROXY_PORT);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/

        /*if ((getWifiName(context).trim()).equalsIgnoreCase("secure-impact")) {
           Log.d(TAG, "----Add Proxy---");
           HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
             Constants.PROXY_PORT);
           params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        }*/

        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

        DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params);

        return mHttpClient;
    }

    public static DefaultHttpClient setHttpPostProxyParams() {

        Log.d(TAG, "() default called ");

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        //schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

        HttpParams params = new BasicHttpParams();

        //Log.d(TAG, "----Add Proxy---");
        /*HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
          Constants.PROXY_PORT);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/

        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

        DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params);

        return mHttpClient;
    }

    public static DefaultHttpClient setHttpPostProxyParams(int connTimeOut) {

        Log.d(TAG, "connTimeout -- called");

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        //schemeRegistry.register(new Scheme("https", new EasySSLSocketFactory(), 443));

        HttpParams params = new BasicHttpParams();

        /*Log.d(TAG, "----Add Proxy---");
        HttpHost proxy = new HttpHost(Constants.PROXY_HOST.trim(),
          Constants.PROXY_PORT);
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);*/

        params.setParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 30);
        params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRouteBean(30));
        params.setParameter(HttpProtocolParams.USE_EXPECT_CONTINUE, false);
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);

        //HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, connTimeOut);
        HttpConnectionParams.setSoTimeout(params, connTimeOut);

        ClientConnectionManager cm = new SingleClientConnManager(params, schemeRegistry);

        DefaultHttpClient mHttpClient = new DefaultHttpClient(cm, params);

        return mHttpClient;
    }
}