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:net.giovannicapuano.galax.util.Utils.java

/**
 * Perform a HTTP POST request.// w  w w  .  j  av  a  2s.  c o  m
 */
public static HttpData postData(String path, List<NameValuePair> post, Context context) {
    HttpParams httpParameters = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
    HttpConnectionParams.setSoTimeout(httpParameters, 10000);

    int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    String body = "";

    HttpClient httpClient = new DefaultHttpClient(httpParameters);
    HttpContext localContext = new BasicHttpContext();

    localContext.setAttribute(ClientContext.COOKIE_STORE, new PersistentCookieStore(context));
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());

    try {
        HttpPost httpPost = new HttpPost(context.getString(R.string.server) + path);
        httpPost.setEntity(new UrlEncodedFormEntity(post));
        HttpResponse response = httpClient.execute(httpPost, localContext);

        status = response.getStatusLine().getStatusCode();
        body = EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new HttpData(status, body);
}

From source file:com.sailthru.android.sdk.impl.external.retrofit.client.ApacheClient.java

private static HttpClient createDefaultClient() {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, Defaults.CONNECT_TIMEOUT_MILLIS);
    HttpConnectionParams.setSoTimeout(params, Defaults.READ_TIMEOUT_MILLIS);
    return new DefaultHttpClient(params);
}

From source file:fossasia.valentina.bodyapp.sync.Sync.java

/**
 * Method which makes all the POST calls
 * //  w  w  w. j  a v  a 2  s .  c o m
 * @param url
 * @param json
 * @return
 */
public String POST(String url, String json, int conTimeOut, int socTimeOut) {

    InputStream inputStream = null;
    String result = "";
    try {

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, conTimeOut);
        HttpConnectionParams.setSoTimeout(httpParameters, socTimeOut);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(url);
        StringEntity se = new StringEntity(json);
        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        HttpResponse httpResponse = httpclient.execute(httpPost);
        inputStream = httpResponse.getEntity().getContent();

        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";
        System.out.println(result + "result");

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

From source file:com.TaxiDriver.jy.DriverQuery.java

public static String getETA(String url, String fromLat, String fromLongi, String toLat, String toLongi,
        String type) {// ww w. j  a  v a  2  s.co  m

    HttpPost postJob = new HttpPost(HttpHelper.domain + "getduration.php");

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used.
    int timeoutConnection = 3000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 4900;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    DefaultHttpClient clientJob = new DefaultHttpClient(httpParameters);

    try {
        List<NameValuePair> infoJob = new ArrayList<NameValuePair>(2);
        infoJob.add(new BasicNameValuePair("url", url));

        infoJob.add(new BasicNameValuePair("fromLongi", fromLongi));
        infoJob.add(new BasicNameValuePair("fromLat", fromLat));
        infoJob.add(new BasicNameValuePair("toLongi", toLongi));
        infoJob.add(new BasicNameValuePair("toLat", toLat));
        infoJob.add(new BasicNameValuePair("type", type));

        postJob.setEntity(new UrlEncodedFormEntity(infoJob));

        HttpResponse response = clientJob.execute(postJob);

        String jsonString = HttpHelper.request(response);
        String duration = jsonString.trim();

        return duration;

    } catch (Exception ex) {
        return null;
    }

}

From source file:com.huguesjohnson.retroleague.util.HttpFetch.java

public static InputStream fetch(String address, int timeout) throws MalformedURLException, IOException {
    HttpGet httpRequest = new HttpGet(URI.create(address));
    HttpClient httpclient = new DefaultHttpClient();
    final HttpParams httpParameters = httpclient.getParams();
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeout);
    HttpConnectionParams.setSoTimeout(httpParameters, timeout);
    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
    InputStream instream = bufHttpEntity.getContent();
    return (instream);
}

From source file:com.avinashbehera.sabera.network.HttpClient.java

public static JSONObject SendHttpPostUsingHttpClient(String URL, JSONObject jsonObjSend) {

    try {/*  www  .  j  av  a2 s . com*/
        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 60 * 1000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 60 * 1000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

        DefaultHttpClient httpclient = new DefaultHttpClient(httpParameters);
        HttpPost httpPostRequest = new HttpPost(URL);

        StringEntity se;
        se = new StringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        //httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");
        //httpPostRequest.setHeader("Accept-Encoding", "gzip"); // only set this parameter if you would like to use gzip compression

        long t = System.currentTimeMillis();
        Log.d(TAG, "httpPostReuest = " + httpPostRequest.toString());
        HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest);
        Log.i(TAG, "HTTPResponse received in [" + (System.currentTimeMillis() - t) + "ms]");

        // Get hold of the response entity (-> the data):
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            // Read the content stream
            InputStream instream = entity.getContent();
            Header contentEncoding = response.getFirstHeader("Content-Encoding");
            if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                instream = new GZIPInputStream(instream);
            }

            // convert content stream to a String
            String resultString = convertStreamToString(instream);
            instream.close();
            resultString = resultString.substring(1, resultString.length() - 1); // remove wrapping "[" and "]"

            // Transform the String into a JSONObject
            JSONParser parser = new JSONParser();
            JSONObject jsonObjRecv = (JSONObject) parser.parse(resultString);
            // Raw DEBUG output of our received JSON object:
            Log.i(TAG, "<JSONObject>\n" + jsonObjRecv.toString() + "\n</JSONObject>");

            return jsonObjRecv;
        }

    } catch (Exception e) {
        Log.d(TAG, "catch block");
        // More about HTTP exception handling in another tutorial.
        // For now we just print the stack trace.
        e.printStackTrace();
    }
    Log.d(TAG, "SendHttpPostUsingHttpClient returning null");
    return null;
}

From source file:HttpConnections.RestConnFactory.java

public ResponseContents RestRequest(HttpRequestBase httprequest) throws IOException {
    HttpResponse httpResponseTemp = null;
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
    HttpConnectionParams.setSoTimeout(httpParams, 30000);
    this.httpclient = new DefaultHttpClient(httpParams);

    try {/*from  w  w  w  .j  a v  a2 s .  co  m*/
        httpResponseTemp = this.httpclient.execute(httprequest);
    } catch (IOException ex) {
        System.out.println("An error occured while executing the request. Message: " + ex);
        this.responseObj.setContents(ex.toString());
    } finally {
        if (httpResponseTemp != null) {
            this.httpResponse = httpResponseTemp;
            this.responseObj.setStatus(this.httpResponse.getStatusLine().toString());
            if (this.httpResponse.getEntity() == null
                    || this.httpResponse.getStatusLine().toString().contains("500")) {
                this.responseObj.setContents("No Content");
            } else {
                this.responseObj.setContents(EntityUtils.toString(this.httpResponse.getEntity()));
            }
        }
        this.httpclient.close();
        return this.responseObj;
    }
}

From source file:com.ilearnrw.reader.utils.HttpHelper.java

public static HttpResponse get(String url) {
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = null;/*  w  ww  . j  a v a 2s .  c o m*/

    HttpConnectionParams.setSoTimeout(client.getParams(), 25000);

    HttpGet get = new HttpGet(url);
    get.setHeader("Authorization", "Basic " + authString);
    try {
        response = client.execute(get);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return response;
}

From source file:Main.java

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);//  w  ww.jav a2 s.c  o  m
    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;
}

From source file:cn.loveapple.client.android.util.ApiUtil.java

public static String getHttpBody(String url, PackageManager packageManager) {
    if (StringUtils.isEmpty(url)) {
        return null;
    }/* w  ww .j  a  va2 s.co  m*/

    String body = null;
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    // SYSTEM INFO
    params.setParameter("", "");
    params.setParameter("", "");
    params.setParameter("", "");
    HttpConnectionParams.setConnectionTimeout(params, 1000);
    HttpConnectionParams.setSoTimeout(params, 1000);
    HttpPost httpRequest = new HttpPost(url);
    HttpResponse httpResponse = null;

    try {
        httpResponse = httpClient.execute(httpRequest);
    } catch (Exception e) {
        Log.e(LOG_TAG, "http response execute failed.", e);
        return null;
    }
    if (httpResponse != null && httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity httpEntity = httpResponse.getEntity();

        try {
            body = EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            Log.e(LOG_TAG, "get http response body failed.", e);
            return null;
        } finally {
            try {
                httpEntity.consumeContent();
            } catch (IOException e) {
            }
        }
    }
    httpClient.getConnectionManager().shutdown();

    return body;
}