Example usage for org.apache.http.client.methods HttpGet HttpGet

List of usage examples for org.apache.http.client.methods HttpGet HttpGet

Introduction

In this page you can find the example usage for org.apache.http.client.methods HttpGet HttpGet.

Prototype

public HttpGet(final String uri) 

Source Link

Usage

From source file:net.lukecollins.dev.github.HttpUtils.java

/**
 * Get Response for Service.//  www .  ja v  a 2 s .c o  m
 * @param service servicename
 * @return returns HttpResponse of Http GET
 */
public static HttpResponse getRequest(String service) {
    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpGet httpGet = new HttpGet(GhatConstantsUtil.URL + service);

    //Add token to header
    String token = GhatConstantsUtil.APIKEY + ":x-oauth-basic";
    String authString = null;
    try {
        authString = "Basic " + Base64.encodeBase64String(token.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException exp) {
        log.error(LOG_CONTEXT, exp);
    }
    httpGet.addHeader("Authorization", authString);

    HttpResponse response = null;
    try {
        response = httpClient.execute(httpGet);
    } catch (IOException exp) {
        log.error(LOG_CONTEXT, exp);
    }

    return response;
}

From source file:middleware.HTTPRequest.java

public static List<Vuelo> doGetVuelos() throws IOException {
    String result;/* w  w w .  j  a v  a  2  s . co  m*/
    String url = "http://localhost:8084/MVIv2/webapi/vuelos";
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpGet getRequest = new HttpGet(url);
    getRequest.addHeader("Accept", "application/json");

    HttpResponse response = httpClient.execute(getRequest);

    if (response.getStatusLine().getStatusCode() != 200) {
        throw new RuntimeException("ERROR AL CONSULTAR DEL TIPO: " + response.getStatusLine().getStatusCode());
    }

    HttpEntity entity = response.getEntity();
    result = EntityUtils.toString(entity);
    List<Vuelo> listaVuelos = getArrayVuelo(result);

    httpClient.getConnectionManager().shutdown();
    return listaVuelos;
}

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 {//from w  ww.j a  v  a2 s  .  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:pt.isel.mpd.util.HttpGw.java

public static <T> T getData(String path, HttpRespFormatter<T> formatter) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
        /*/*from  w  w w .  j ava  2  s .  c  o  m*/
        * Method: HttpGet
        */
        HttpGet httpget = new HttpGet(path);
        /*
        * HttpResponse
        */
        try (CloseableHttpResponse response = httpclient.execute(httpget)) {

            return formatter.format(response.getEntity());
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.mycompany.trader.HTTPTransport.java

public static HttpGet createGet(String resource, String userId, String hashedSig, String timeStamp,
        String nonce) {// ww  w  .j  av a  2s .  c o  m
    try {
        HttpGet get = new HttpGet(url + resource);
        get.setHeader("Content-Type", "application/json");
        get.addHeader("Authorization", userId + ":" + hashedSig);
        get.addHeader("x-timestamp", timeStamp);
        get.addHeader("nonce", nonce);
        return get;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.hat.tools.HttpUtils.java

public static void SendRequest(String url, Handler handler) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();

    byte[] data = null;
    InputStream in = null;//  ww w. j av  a 2s  . c  o  m
    try {
        HttpResponse response = httpClient.execute(request);
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            in = response.getEntity().getContent();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            data = out.toByteArray();
            Message msg = new Message();
            msg.what = HTTP_FINISH;
            Bundle bundle = new Bundle();
            bundle.putByteArray("data", data);
            msg.setData(bundle);
            handler.sendMessage(msg);
            out.close();
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {

        try {
            if (in != null)
                in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.npr.android.news.DownloadDrawable.java

public static Drawable createFromUrl(String url) {
    InputStream data = null;//from w w w  .  j a  va  2s  .c  om
    Log.d(LOG_TAG, "Starting download");
    HttpClient http = new DefaultHttpClient();
    HttpGet method = new HttpGet(url);

    HttpResponse response = null;
    try {
        response = http.execute(method);
        data = response.getEntity().getContent();
    } catch (ClientProtocolException e) {
        Log.e(LOG_TAG, "error downloading", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "error downloading", e);
    } catch (IllegalStateException e) {
        Log.e(LOG_TAG, "error downloading", e);
    }
    Log.d(LOG_TAG, "Download complete");
    return Drawable.createFromStream(data, url);
}

From source file:com.leprechaun.solveig.http.RequestExecutor.java

public static ResponseEntity simpleExecutor(String host, String port, String query) {

    ResponseEntity responseEntity = new ResponseEntity();

    HttpClient client = new DefaultHttpClient();

    String url = "http://" + host + ":" + port + "/query?q=" + query;

    System.out.println(url);//from w ww .  j  a  v  a2 s  .com

    HttpGet get = new HttpGet(url);

    try {
        HttpResponse response = client.execute(get);
        HttpEntity entity = response.getEntity();
        String responseString = EntityUtils.toString(entity, "UTF-8");
        System.out.println(responseString);

        responseEntity.setCode(response.getStatusLine().toString());
        responseEntity.setBody(responseString);
    } catch (IOException e) {
        System.out.println(e.getMessage());

        responseEntity.setCode("ERROR");
        responseEntity.setBody(e.getMessage());
    }

    return responseEntity;
}

From source file:com.codingrhemes.steamsalesmobile.HttpThumbnails.java

public static Bitmap readPictureFromTheWeb(String URL) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    Bitmap thePicture = null;//w w w .ja  v a2  s.  c o m

    try {

        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            thePicture = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } else {
            Log.d("JSON", "Failed to download file");
        }
    } catch (Exception e) {
        Log.d("HttpThumbnails", e.getLocalizedMessage());
    }
    return thePicture;
}

From source file:org.moneta.utils.RestTestingUtils.java

public static HttpResponse simpleRESTGet(String requestUri) {
    Validate.notEmpty(requestUri, "Null or blank requestUri not allowed.");
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet(requestUri);
    try {//from   w w  w  . j a va  2 s.co m
        return httpclient.execute(httpGet);
    } catch (Exception e) {
        throw new ContextedRuntimeException(e).addContextValue("requestUri", requestUri);
    }
}