Example usage for org.apache.http.client HttpClient getParams

List of usage examples for org.apache.http.client HttpClient getParams

Introduction

In this page you can find the example usage for org.apache.http.client HttpClient getParams.

Prototype

@Deprecated
HttpParams getParams();

Source Link

Document

Obtains the parameters for this client.

Usage

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static JsonNode postToServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;//  w ww.  j a v a  2s.c  om
    LOG.d("POST api request, url = " + urlString + " object = " + objectToPost.toString());
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpPost httppost = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httppost = new HttpPost(url.toString());
        httppost.setHeader("Content-type", "application/json");
        httppost.setHeader("Accept", ACCEPT);
        httppost.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);// , HTTP.UTF_8
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:com.spoiledmilk.ibikecph.util.HttpUtils.java

public static JsonNode putToServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;/*from ww w .j  a v  a2s .  c  o m*/
    LOG.d("PUT api request, url = " + urlString);
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpPut httput = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httput = new HttpPut(url.toString());
        httput.setHeader("Content-type", "application/json");
        httput.setHeader("Accept", ACCEPT);
        httput.setHeader("LANGUAGE_CODE", IbikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httput.setEntity(se);
        HttpResponse response = httpclient.execute(httput);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:com.upyun.sdk.utils.HttpClientUtils.java

private static HttpResponse post(String url, HttpClient httpclient, Map<String, String> headers) {

    HttpResponse response = null;//ww w.j av a  2s.c  o m

    try {
        httpclient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 
        httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, SO_TIMEOUT); // ?

        HttpPost httpPost = new HttpPost(url);
        for (String key : headers.keySet()) {
            httpPost.addHeader(key, headers.get(key));
        }
        response = httpclient.execute(httpPost);
    } catch (Exception e) {
        LogUtil.exception(logger, e);
    }

    return response;
}

From source file:com.upyun.sdk.utils.HttpClientUtils.java

private static HttpResponse delete(String url, HttpClient httpclient, Map<String, String> headers) {

    HttpResponse response = null;/*w ww.java2 s  .  c o  m*/

    try {
        httpclient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 
        httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, SO_TIMEOUT); // ?

        HttpDelete httpDelete = new HttpDelete(url);

        for (String key : headers.keySet()) {
            httpDelete.addHeader(key, headers.get(key));
        }

        response = httpclient.execute(httpDelete);
    } catch (Exception e) {
        LogUtil.exception(logger, e);
    }

    return response;
}

From source file:vn.evolus.droidreader.util.ImageCache.java

public static void downloadImage(String imageUrl) throws IOException {
    HttpClient client = new DefaultHttpClient();
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, ImageLoader.CONNECT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, ImageLoader.READ_TIMEOUT);
    HttpGet httpGet = new HttpGet(imageUrl);
    HttpResponse response = client.execute(httpGet);
    InputStream is = response.getEntity().getContent();

    // save to cache folder
    if (Constants.DEBUG_MODE)
        Log.d(Constants.LOG_TAG, "Save image to " + getCacheFileName(imageUrl));
    File imageFile = new File(getCacheFileName(imageUrl));
    imageFile.createNewFile();/*from  w ww.  j a  va  2 s. c  o  m*/
    FileOutputStream os = null;
    try {
        os = new FileOutputStream(imageFile);
        StreamUtils.writeStream(is, os);
    } finally {
        if (os != null) {
            os.close();
        }
    }
}

From source file:com.upyun.sdk.utils.HttpClientUtils.java

private static HttpResponse get(String url, String inputParam, HttpClient httpclient,
        Map<String, String> headers) {

    HttpResponse response = null;/*from   ww w .j a va  2 s . c  o  m*/

    try {
        httpclient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, CONNECTION_TIMEOUT);// 
        httpclient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, SO_TIMEOUT); // ?

        HttpGet httpGet;
        if (inputParam != null && inputParam.length() > 0) {
            httpGet = new HttpGet(url + "?" + inputParam);
        } else {
            httpGet = new HttpGet(url);
        }

        for (String key : headers.keySet()) {
            httpGet.addHeader(key, headers.get(key));
        }

        response = httpclient.execute(httpGet);
    } catch (Exception e) {
        LogUtil.exception(logger, e);
    }

    return response;
}

From source file:org.thoughtcrime.securesms.mms.MmsDownloadHelper.java

private static byte[] makeRequest(MmsConnectionParameters connectionParameters, String url)
        throws ClientProtocolException, IOException {
    try {/*ww  w.j  av  a2  s .c  o m*/
        HttpClient client = constructHttpClient(connectionParameters);
        URI hostUrl = new URI(url);
        HttpHost target = new HttpHost(hostUrl.getHost(), hostUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpRequest request = new HttpGet(url);

        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsDownlader", use);
        throw new IOException("Bad URI syntax");
    }
}

From source file:io.github.data4all.handler.TagSuggestionHandler.java

/**
 * /*from  w  w  w.j  a  v  a2s. co m*/
 * @param url
 * @return the JSONObject from an url
 */
private static JSONObject getJSONfromURL(String url) {

    // initialize
    InputStream is = null;
    String address = "";
    JSONObject jObject = null;

    // http post
    try {
        final HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, System.getProperty("http.agent"));
        final HttpGet httppost = new HttpGet(url);
        final HttpResponse response = httpclient.execute(httppost);
        final HttpEntity entity = response.getEntity();
        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }

    // convert response to string
    try {
        final BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        address = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting address " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObject = new JSONObject(address);
    } catch (JSONException e) {
        Log.e("log_tag", "Error parsing data [" + e.getMessage() + "] " + address);
    }
    return jObject;
}

From source file:dk.kk.ibikecphlib.util.HttpUtils.java

public static JsonNode getFromServer(String urlString) {
    JsonNode ret = null;/* w ww.j av  a2s . c om*/
    LOG.d("GET api request, url = " + urlString);
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HttpGet httpget = null;
    URL url = null;
    try {

        url = new URL(urlString);
        httpget = new HttpGet(url.toString());
        httpget.setHeader("Content-type", "application/json");
        httpget.setHeader("Accept", ACCEPT);
        httpget.setHeader("LANGUAGE_CODE", IBikeApplication.getLanguageString());
        HttpResponse response = httpclient.execute(httpget);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null) {
            LOG.e(e.getLocalizedMessage());
        }
    }
    return ret;
}

From source file:dk.kk.ibikecphlib.util.HttpUtils.java

public static JsonNode deleteFromServer(String urlString, JSONObject objectToPost) {
    JsonNode ret = null;/*from w ww .j av a2s  .c o m*/
    LOG.d("DELETE api request, url = " + urlString);
    HttpParams myParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myParams, CONNECTON_TIMEOUT);
    HttpConnectionParams.setSoTimeout(myParams, CONNECTON_TIMEOUT);
    HttpClient httpclient = new DefaultHttpClient(myParams);
    httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, Config.USER_AGENT);
    HTTPDeleteWithBody httpdelete = null;
    URL url = null;
    try {
        url = new URL(urlString);
        httpdelete = new HTTPDeleteWithBody(url.toString());
        httpdelete.setHeader("Content-type", "application/json");
        httpdelete.setHeader("Accept", ACCEPT);
        httpdelete.setHeader("LANGUAGE_CODE", IBikeApplication.getLanguageString());
        StringEntity se = new StringEntity(objectToPost.toString(), HTTP.UTF_8);
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httpdelete.setEntity(se);
        HttpResponse response = httpclient.execute(httpdelete);
        String serverResponse = EntityUtils.toString(response.getEntity());
        LOG.d("API response = " + serverResponse);
        ret = Util.stringToJsonNode(serverResponse);
    } catch (Exception e) {
        if (e != null && e.getLocalizedMessage() != null)
            LOG.e(e.getLocalizedMessage());
    }
    return ret;
}