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:me.sandrin.xkcdwidget.GetJsonTask.java

@Override
protected String doInBackground(String... params) {

    String url = params[0];//from   www .  j  a v  a2  s  .  c o  m
    String jsonText = null;
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        InputStream is = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        jsonText = reader.readLine();
    } catch (IOException e) {
        Log.e("XKCD", "Unable to get XKCD Json.", e);
    }

    return jsonText;
}

From source file:org.opencastproject.remotetest.server.resource.WorkflowResources.java

/**
 * //from  www  . j a v a 2s  .  c  o  m
 * @param format Response format: xml or json
 * 
 */
public static HttpResponse instances(TrustedHttpClient client, String format) throws Exception {
    return client.execute(new HttpGet(getServiceUrl() + "instances." + format.toLowerCase()));
}

From source file:de.avanux.android.livetracker2.HttpUtil.java

public static String get(String url) throws ClientProtocolException, IOException {
    Log.d(TAG, "HTTP GET " + url);
    HttpGet method = new HttpGet(url);
    HttpResponse response = executeMethod(method);
    return getResponseAsString(response);
}

From source file:com.groupon.odo.tests.HttpUtils.java

public static String doProxyGet(String url, BasicNameValuePair[] data) throws Exception {
    String fullUrl = url;/*from   w w  w.  j ava 2s  .  c  o  m*/

    if (data != null) {
        if (data.length > 0) {
            fullUrl += "?";
        }

        for (BasicNameValuePair bnvp : data) {
            fullUrl += bnvp.getName() + "=" + uriEncode(bnvp.getValue()) + "&";
        }
    }

    HttpGet get = new HttpGet(fullUrl);
    int port = Utils.getSystemPort(Constants.SYS_HTTP_PORT);
    HttpHost proxy = new HttpHost("localhost", port);
    HttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
    client.getParams().setParameter(org.apache.http.conn.params.ConnRoutePNames.DEFAULT_PROXY, proxy);
    HttpResponse response = client.execute(get);

    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String accumulator = "";
    String line = "";
    Boolean firstLine = true;
    while ((line = rd.readLine()) != null) {
        accumulator += line;
        if (!firstLine) {
            accumulator += "\n";
        } else {
            firstLine = false;
        }
    }
    return accumulator;
}

From source file:com.handlerexploit.news.data.YQLHelper.java

public static String query(String query) {
    String fullUrl = null;//from  w w  w .  jav  a 2s .co m
    try {
        fullUrl = "http://query.yahooapis.com/v1/public/yql?format=json&q="
                + URLEncoder.encode(query, "US-ASCII");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "Error encoding URL", e);
    }
    if (fullUrl != null) {
        String queryResponse = null;
        try {
            String userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
            AndroidHttpClient androidHttpClient = AndroidHttpClient.newInstance(userAgent); // TODO Look into using something else for pre-froyo
            queryResponse = EntityUtils.toString(androidHttpClient.execute(new HttpGet(fullUrl)).getEntity());
            androidHttpClient.close();
            JSONObject jsonQuery = new JSONObject(queryResponse).getJSONObject("query");
            if (jsonQuery.getInt("count") > 0) {
                return jsonQuery.getJSONObject("results").toString();
            } else {
                Log.d(TAG, "YQL returned empty - " + fullUrl + " - " + jsonQuery.toString());
            }
        } catch (Throwable e) {
            Log.e(TAG, "An error occured while parsing yql query - " + queryResponse, e);
        }
    }
    return null;
}

From source file:eu.scape_project.fcrepo.integration.AbstractIT.java

@BeforeClass
public static void init() throws Exception {
    /* wait for an existing ContainerWrapper to finish */
    try {//from w w  w .ja  v a2 s.com
        HttpGet get = new HttpGet("http://localhost:8080");
        DefaultHttpClient client = new DefaultHttpClient();
        while (client.execute(get).getStatusLine().getStatusCode() > 0) {
            Thread.sleep(1000);
            System.out.println("Waiting for existing application server to stop...");
        }
    } catch (ConnectException e) {
        // it's good, we can't connect to 8080
        // don't do exec flow by exception handling though ;)
    }
}

From source file:at.uni_salzburg.cs.ckgroup.cscpp.utils.HttpQueryUtils.java

public static String simpleQuery(String url) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;/*from   w  w  w . j  a v  a 2s. c  om*/

    response = httpclient.execute(httpget);
    ByteArrayOutputStream bo = new ByteArrayOutputStream();

    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        int l;
        byte[] tmp = new byte[2048];
        while ((l = instream.read(tmp)) != -1) {
            bo.write(tmp, 0, l);
        }
    }

    return bo.toString();
}

From source file:org.winardiaris.uangku.getDataURL.java

String getData(String url) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    try {//ww w .  ja  va  2s .  com
        HttpGet httpget = new HttpGet(url);
        System.out.println("Executing request " + httpget.getRequestLine());

        ResponseHandler<String> responseHandler;
        responseHandler = new ResponseHandler<String>() {

            @Override
            public String handleResponse(final HttpResponse response)
                    throws ClientProtocolException, IOException {
                int status = response.getStatusLine().getStatusCode();
                if (status >= 200 && status < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity != null ? EntityUtils.toString(entity) : null;
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + status);
                }
            }
        };

        String responseURL = httpclient.execute(httpget, responseHandler);
        return responseURL;
    } finally {
        httpclient.close();
    }
}

From source file:com.football.site.getdata.ScoreWebService.java

private static String GetHttpClientResponse(String url) {
    String responseText = "";
    try {//from w  w w  . j a v a 2s  .  c  o m
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet httpGet = new HttpGet(url);
            httpGet.addHeader("X-Auth-Token", Constants.XAuthToken);
            httpGet.addHeader("Content-type", "application/json");
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            responseText = EntityUtils.toString(httpResponse.getEntity(), "ISO-8859-1");
            //logger.info(String.format("%s - %s", url, responseText));
        }
    } catch (IOException | ParseException e) {
        HelperUtil.AddErrorLog(logger, e);
        HelperUtil.AddErrorLogWithString(logger, url);
    }
    return responseText;
}

From source file:com.zyf.bike.suzhou.utils.HttpOperation.java

/**
 * Http Get??//from w  w  w  .ja  v  a  2  s.  co m
 * @param url
 * @param testnum ?
 * @return
 */
public static byte[] doGetBase(String url, int testnum) {
    if (testnum < 0) {
        return null;
    }
    try {
        //HttpClient    
        HttpClient httpclient = new DefaultHttpClient();
        //POST  
        HttpGet get = new HttpGet(url);
        HttpResponse response = httpclient.execute(get);
        int code = response.getStatusLine().getStatusCode();
        if (code == HttpStatus.SC_OK) {
            //?
            return EntityUtils.toByteArray(response.getEntity());
        } else if (code == HttpStatus.SC_NOT_FOUND || code == HttpStatus.SC_FORBIDDEN
                || code == HttpStatus.SC_METHOD_NOT_ALLOWED) {
            //403 ?
            //404 ??
            //405 ??
            return null;
        } else {
            //??
            doGetBase(url, testnum--);
        }
    } catch (Exception e) {
        Logger.e(TAG, e.getMessage(), e);
    }
    return null;
}