Example usage for org.apache.http.params HttpConnectionParams setSoTimeout

List of usage examples for org.apache.http.params HttpConnectionParams setSoTimeout

Introduction

In this page you can find the example usage for org.apache.http.params HttpConnectionParams setSoTimeout.

Prototype

public static void setSoTimeout(HttpParams httpParams, int i) 

Source Link

Usage

From source file:eu.masconsult.bgbanking.banks.fibank.my.MyFIBankClient.java

/**
 * Configures the httpClient to connect to the URL provided.
 *//*  ww w.j  av a2 s.c  om*/
private static DefaultHttpClient getHttpClient() {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    final HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    HttpConnectionParams.setSoTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    ConnManagerParams.setTimeout(params, HTTP_REQUEST_TIMEOUT_MS);
    // HttpClientParams.setRedirecting(params, false);
    httpClient.addRequestInterceptor(new DumpHeadersRequestInterceptor());
    httpClient.addResponseInterceptor(new DumpHeadersResponseInterceptor());
    return httpClient;
}

From source file:com.piusvelte.sonet.core.SonetHttpClient.java

protected static DefaultHttpClient getThreadSafeClient(Context context) {
    if (sHttpClient == null) {
        Log.d(TAG, "create http client");
        SocketFactory sf;//from   w w w . j  a va2 s  . c o  m
        try {
            Class<?> sslSessionCacheClass = Class.forName("android.net.SSLSessionCache");
            Object sslSessionCache = sslSessionCacheClass.getConstructor(Context.class).newInstance(context);
            Method getHttpSocketFactory = Class.forName("android.net.SSLCertificateSocketFactory")
                    .getMethod("getHttpSocketFactory", new Class<?>[] { int.class, sslSessionCacheClass });
            sf = (SocketFactory) getHttpSocketFactory.invoke(null, CONNECTION_TIMEOUT, sslSessionCache);
        } catch (Exception e) {
            Log.e("HttpClientProvider",
                    "Unable to use android.net.SSLCertificateSocketFactory to get a SSL session caching socket factory, falling back to a non-caching socket factory",
                    e);
            sf = SSLSocketFactory.getSocketFactory();
        }
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        HttpParams params = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
        HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
        String versionName;
        try {
            versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (NameNotFoundException e) {
            throw new RuntimeException(e);
        }
        StringBuilder userAgent = new StringBuilder();
        userAgent.append(context.getPackageName());
        userAgent.append("/");
        userAgent.append(versionName);
        userAgent.append(" (");
        userAgent.append("Linux; U; Android ");
        userAgent.append(Build.VERSION.RELEASE);
        userAgent.append("; ");
        userAgent.append(Locale.getDefault());
        userAgent.append("; ");
        userAgent.append(Build.PRODUCT);
        userAgent.append(")");
        if (HttpProtocolParams.getUserAgent(params) != null) {
            userAgent.append(" ");
            userAgent.append(HttpProtocolParams.getUserAgent(params));
        }
        HttpProtocolParams.setUserAgent(params, userAgent.toString());
        sHttpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
    }
    return sHttpClient;
}

From source file:com.momock.service.HttpService.java

@Override
public void start() {
    httpClient = AndroidHttpClient.newInstance(userAgent);
    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), SOCKET_OPERATION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpClient.getParams(), SOCKET_OPERATION_TIMEOUT);
}

From source file:io.undertow.testutils.TestHttpClient.java

@Override
protected HttpParams createHttpParams() {
    HttpParams params = super.createHttpParams();
    HttpConnectionParams.setSoTimeout(params, 30000);
    return params;
}

From source file:eu.geopaparazzi.library.routing.osmbonuspack.HttpConnection.java

public HttpConnection() {
    stream = null;/* www  . j av a2s  .com*/
    entity = null;
    HttpParams httpParameters = new BasicHttpParams();
    /* useful?
    HttpProtocolParams.setContentCharset(httpParameters, "UTF-8"); 
    HttpProtocolParams.setHttpElementCharset(httpParameters, "UTF-8");
    */
    // Set the timeout in milliseconds until a connection is established.
    HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT_CONNECTION);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT_SOCKET);
    client = new DefaultHttpClient(httpParameters);
    //TODO: created here. Reuse to do for better perfs???...
}

From source file:ws.kotonoha.android.services.DataService.java

@Override
public void onCreate() {
    confSvc = new ConfigService(this);
    confSvc.load();/*from   w w w . j a va 2 s  .  c  om*/
    super.onCreate();
    httpClient = AndroidHttpClient.newInstance("Kotonoha/1.0");

    HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 3000);
    HttpConnectionParams.setSoTimeout(httpClient.getParams(), 10000);

    markSvc = new MarkService(this);
    cardSvc = new CardService(this);
    wordSvc = new WordService(this);
    eventSvc = new EventService(this);

    AuthObject ao = confSvc.config().getAuthObject();
    if (ao != null) {
        createRestSvc(ao);
    }

    esReg = new EventualSvcRegistry(this);
    Scheduler.delayed(esReg, Duration.standardSeconds(30));
}

From source file:net.oneandone.sushi.fs.webdav.WebdavRoot.java

public void setSoTimeout(int millis) {
    HttpConnectionParams.setSoTimeout(params, millis);
}

From source file:com.attentec.ServerContact.java

/**
 * Posts POST request to url with data that from values.
 * Values are put together to a json object before they are sent to server.
 * postname = { subvar_1_name => subvar_1_value,
 *             subvar_2_name => subvar_2_value
 *             ...}/*from   w ww . jav  a2  s .  c o  m*/
 * postname_2 = {...}
 * ...
 * @param values hashtable with structure:
 * @param url path on the server to call
 * @param ctx calling context
 * @return JSONObject with data from rails server.
 * @throws Login.LoginException when login is wrong
 */
public static JSONObject postJSON(final Hashtable<String, List<NameValuePair>> values, final String url,
        final Context ctx) throws Login.LoginException {
    //fetch the urlbase
    urlbase = PreferencesHelper.getServerUrlbase(ctx);

    //create a JSONObject of the hashtable
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    Enumeration<String> postnames = values.keys();

    String postname;
    List<NameValuePair> postvalues;
    JSONObject postdata;

    while (postnames.hasMoreElements()) {
        postname = (String) postnames.nextElement();
        postvalues = values.get(postname);
        postdata = new JSONObject();
        for (int i = 0; i < postvalues.size(); i++) {
            try {
                postdata.put(postvalues.get(i).getName(), postvalues.get(i).getValue());
            } catch (JSONException e) {
                Log.w(TAG, "JSON fail");
                return null;
            }
        }
        pairs.add(new BasicNameValuePair(postname, postdata.toString()));
    }

    //prepare the http call
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, CONNECTION_TIMEOUT);

    HttpClient client = new DefaultHttpClient(httpParams);

    HttpPost post = new HttpPost(urlbase + url);
    //Log.d(TAG, "contacting url: " + post.getURI());
    try {
        post.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        return null;
    }

    //call the server
    String response = null;
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    try {
        response = client.execute(post, responseHandler);
    } catch (IOException e) {
        Log.e(TAG, "Failed in HTTP request: " + e.toString());
        return null;
    }
    //Log.d(TAG, "Have contacted url success: " + post.getURI());
    //read response
    JSONObject jsonresponse;
    try {

        jsonresponse = new JSONObject(response);
    } catch (JSONException e) {
        Log.e(TAG, "Incorrect response from server" + e.toString());
        return null;
    }
    String responsestatus;
    try {
        responsestatus = jsonresponse.getString("Responsestatus");
    } catch (JSONException e1) {
        return null;
    }
    if (!responsestatus.equals("Wrong login")) {
        return jsonresponse;
    } else {
        Log.w(TAG, "Wrong login");
        throw new Login.LoginException("Wrong login");
    }
}

From source file:com.oakclub.android.base.OakClubApplication.java

private void pingActivities() {
    Thread SplashTimer = new Thread() {
        public void run() {
            try {
                while (true) {
                    HttpClient hClient = new DefaultHttpClient();
                    HttpConnectionParams.setConnectionTimeout(hClient.getParams(), 20000);
                    HttpConnectionParams.setSoTimeout(hClient.getParams(), 60000);
                    try {
                        HttpGet hget = new HttpGet();
                        String headerValue = "UsernameToken " + "Username=\""
                                + OakClubBaseActivity.facebook_user_id + "\", AccessToken=\""
                                + OakClubBaseActivity.access_token
                                + "\", Nonce=\"1ifn7s\", Created=\"2013-10-19T07:12:43.407Z\"";
                        hget.setHeader(Constants.HEADER_X_WSSE, headerValue);
                        hget.setHeader(Constants.HEADER_ACCEPT, "application/json");
                        hget.setHeader(Constants.HEADER_ACCEPT, "text/html");
                        hget.setHeader(Constants.HTTP_USER_AGENT, "Android");
                        hget.setURI(new URI(getApplicationContext().getString(R.string.default_server_address)
                                + "/pingActivities"));
                        HttpResponse response = hClient.execute(hget);
                        StatusLine statusLine = response.getStatusLine();

                        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                            ByteArrayOutputStream out = new ByteArrayOutputStream();
                            response.getEntity().writeTo(out);
                            out.close();
                        }/*  w ww .j  a  v  a  2s.c  o m*/
                    } catch (ConnectTimeoutException e) {
                    } catch (Exception e) {
                    }
                    sleep(60000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    SplashTimer.start();
}

From source file:org.cgiar.ilri.odk.pull.backend.DataHandler.java

/**
 * Performs a HTTP/HTTPS request to the server
 *
 * @param context       Context e.g activity that is making request
 * @param jsonString    The {@link org.json.JSONObject} or {@link org.json.JSONArray} string to
 *                      be sent to the server
 * @param appendedURL   The URI to be appended to the BASE_URL
 *
 * @return  String containing the response from the server or null if an error occurs
 *///from   ww  w  .j av  a2s  . co  m
public static String sendDataToServer(Context context, String jsonString, String appendedURL) {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, HTTP_POST_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, HTTP_RESPONSE_TIMEOUT);
    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpGet httpGet = new HttpGet(BASE_URL + appendedURL);
    try {
        //List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(1);
        //nameValuePairs.add(new BasicNameValuePair("json", jsonString));
        //httpGet.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse httpResponse = httpClient.execute(httpGet);
        if (httpResponse.getStatusLine().getStatusCode() == 200) {
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                InputStream inputStream = httpEntity.getContent();
                String responseString = convertStreamToString(inputStream);
                return responseString.trim();
            }
        } else {
            Log.e(TAG,
                    "Status Code " + String.valueOf(httpResponse.getStatusLine().getStatusCode()) + " passed");
        }
    } catch (Exception e) {
        e.printStackTrace();
        setSharedPreference(context, "http_error", e.getMessage());
    }
    if (isConnectedToServer(HTTP_POST_TIMEOUT)) {
        setSharedPreference(context, "http_error",
                "This application was unable to reach http://azizi.ilri.cgiar.org within "
                        + String.valueOf(HTTP_POST_TIMEOUT / 1000)
                        + " seconds. Try resetting your network connection");
    }
    return null;
}