Example usage for org.apache.http.impl.client DefaultHttpClient execute

List of usage examples for org.apache.http.impl.client DefaultHttpClient execute

Introduction

In this page you can find the example usage for org.apache.http.impl.client DefaultHttpClient execute.

Prototype

public CloseableHttpResponse execute(final HttpUriRequest request) throws IOException, ClientProtocolException 

Source Link

Usage

From source file:neembuu.release1.httpclient.utils.NHttpClientUtils.java

/**
 * Get the content of a page./*  ww w  .  jav a2s . co m*/
 * @param url url from which to read
 * @return the String content of the page
 * @throws Exception 
 */
public static String getData(String url, DefaultHttpClient httpClient) throws Exception {
    HttpGet httpGet = new HttpGet(url);
    HttpResponse httpResponse = httpClient.execute(httpGet);
    return EntityUtils.toString(httpResponse.getEntity());
}

From source file:com.ammobyte.radioreddit.api.InternetCommunication.java

public static InputStream retrieveStream(String url) {

    DefaultHttpClient client = new DefaultHttpClient();

    HttpGet getRequest = new HttpGet(url);

    try {// ww  w.j  a v a 2s . co  m

        HttpResponse getResponse = client.execute(getRequest);
        final int statusCode = getResponse.getStatusLine().getStatusCode();

        if (statusCode != HttpStatus.SC_OK) {
            Log.w("InternetConnection", "Error " + statusCode + " for URL " + url);
            return null;
        }

        HttpEntity getResponseEntity = getResponse.getEntity();
        return getResponseEntity.getContent();

    } catch (IOException e) {
        getRequest.abort();
        Log.w("InternetConnection", "Error for URL " + url, e);
    }

    return null;

}

From source file:com.photon.phresco.Screens.HttpRequest.java

/**
 * Send the JSON data to specified URL and get the httpResponse back
 *
 * @param sURL//from   ww  w .ja  v  a 2 s . co  m
 * @param jObject
 * @return InputStream
 * @throws IOException
 */
public static InputStream post(String sURL, JSONObject jObject) throws IOException {
    HttpResponse httpResponse = null;
    InputStream is = null;

    HttpPost httpPostRequest = new HttpPost(sURL);
    HttpEntity entity;

    httpPostRequest.setHeader("Accept", "application/json");
    httpPostRequest.setHeader(HTTP.CONTENT_TYPE, "application/json");
    httpPostRequest.setEntity(new ByteArrayEntity(jObject.toString().getBytes("UTF-8")));
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = TIME_OUT;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = TIME_OUT;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    httpResponse = httpClient.execute(httpPostRequest);

    if (httpResponse != null) {
        entity = httpResponse.getEntity();
        is = entity.getContent();
    }
    return is;
}

From source file:org.apache.usergrid.apm.service.UsergridInternalRestServerConnector.java

public static boolean isCrashNotificationDisabled(String orgName, String appName) {
    String restServerUrl = DeploymentConfig.geDeploymentConfig().getUsergridRestUrl();
    //String restServerUrl = "http://instaops-apigee-mobile-app-prod.apigee.net/";
    restServerUrl += "/" + orgName + "/" + appName + "/apm/apigeeMobileConfig";
    log.info("Checking if crash notification is disabled for " + orgName + " app : " + appName);

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getMethod = new HttpGet(restServerUrl);
    HttpResponse response;//from  www  .jav a 2 s  .c o m
    try {
        response = httpClient.execute(getMethod);

        HttpEntity entity = response.getEntity();
        String jsonObject = EntityUtils.toString(entity);
        ObjectMapper mapper = new ObjectMapper();
        App app = mapper.readValue(jsonObject, App.class);
        Set<AppConfigCustomParameter> parameters = app.getDefaultAppConfig().getCustomConfigParameters();
        for (AppConfigCustomParameter param : parameters) {
            if (param.getTag().equalsIgnoreCase("ALARM")
                    && param.getParamKey().equalsIgnoreCase("SUPPRESS_ALARMS")
                    && param.getParamValue().equalsIgnoreCase("TRUE"))
                ;
            {
                return true;
            }
        }

    } catch (ClientProtocolException e) {
        log.error("Problem connectiong to Usergrid internal REST server " + restServerUrl);
        e.printStackTrace();

    } catch (IOException e) {
        log.error("Problem connectiong to Usergrid internal REST server " + restServerUrl);
        e.printStackTrace();
    }
    httpClient = null;
    return false;
}

From source file:com.photon.phresco.Screens.HttpRequest.java

/**
 * Get the content from web url, and parse it using json parser
 *
 * @param sURL/*w  ww  .j ava 2 s  .c om*/
 *            : URL to hit to get the json content
 * @return InputStream
 * @throws IOException
 */
public static InputStream get(String sURL) throws IOException {

    InputStream is = null;
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = TIME_OUT;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = TIME_OUT;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpGet httpGet = new HttpGet(sURL);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity entity = httpResponse.getEntity();
    is = entity.getContent();

    return is;
}

From source file:com.mono.applink.Twitter.java

public static HttpResponse simpleHttp(HttpGet HttpGet) {
    HttpResponse response = null;//from   w  ww. j a  v a 2  s .  co m
    DefaultHttpClient client = new DefaultHttpClient();
    try {
        response = client.execute(HttpGet);

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    return response;
}

From source file:com.photon.phresco.nativeapp.eshop.net.HttpRequest.java

/**
 * Send the JSON data to specified URL and get the httpResponse back
 *
 * @param sURL/*from  w ww .j  av  a  2 s  .  com*/
 * @param jObject
 * @return InputStream
 * @throws IOException
 */
public static InputStream post(String sURL, JSONObject jObject) throws IOException {
    HttpResponse httpResponse = null;
    InputStream is = null;

    PhrescoLogger.info(TAG + " post: " + sURL);
    PhrescoLogger.info(TAG + " jObject: " + jObject);

    HttpPost httpPostRequest = new HttpPost(sURL);
    HttpEntity entity;

    httpPostRequest.setHeader("Accept", "application/json");
    httpPostRequest.setHeader(HTTP.CONTENT_TYPE, "application/json");
    httpPostRequest.setEntity(new ByteArrayEntity(jObject.toString().getBytes("UTF-8")));
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = TIME_OUT;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = TIME_OUT;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
    httpResponse = httpClient.execute(httpPostRequest);

    if (httpResponse != null) {
        entity = httpResponse.getEntity();
        is = entity.getContent();
    }
    return is;
}

From source file:Main.java

public static String getStringFromUrl(List<NameValuePair> nameValuePairs)
        throws ClientProtocolException, IOException {
    String url = "http://www.fsurugby.org/serve/request.php";
    DefaultHttpClient client = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = client.execute(httppost);
    HttpEntity entity = response.getEntity();
    BufferedHttpEntity buffer = new BufferedHttpEntity(entity);
    InputStream is = buffer.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();
    String line;//  w w  w. j a v  a2 s . c om
    while ((line = reader.readLine()) != null) {
        sb.append(line);
    }

    return sb.toString();
}

From source file:Main.java

public static String getHttpClientString(String path) {
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
    HttpClient client = new DefaultHttpClient(httpParams);
    DefaultHttpClient httpClient = (DefaultHttpClient) client;
    HttpResponse httpResponse = null;//from  www .j  a  v a  2s  . c  o m
    String result = "";
    try {
        httpResponse = httpClient.execute(new HttpGet(path));
        int res = httpResponse.getStatusLine().getStatusCode();
        if (res == 200) {
            InputStream in = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in, "GBK"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line.trim());
            }
            reader.close();
            in.close();
            result = sb.toString();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return result;
}

From source file:com.photon.phresco.nativeapp.eshop.net.HttpRequest.java

/**
 * Get the content from web url, and parse it using json parser
 *
 * @param sURL//from   w  w w  . j ava  2  s .  c o  m
 *            : URL to hit to get the json content
 * @return InputStream
 * @throws IOException
 */
public static InputStream get(String sURL) throws IOException {

    PhrescoLogger.info(TAG + "get: " + sURL);
    InputStream is = null;
    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    int timeoutConnection = TIME_OUT;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT)
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = TIME_OUT;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
    HttpGet httpGet = new HttpGet(sURL);
    DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);

    HttpResponse httpResponse = httpClient.execute(httpGet);
    HttpEntity entity = httpResponse.getEntity();
    is = entity.getContent();

    return is;
}