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() 

Source Link

Usage

From source file:Main.java

public static String responseContent(String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.setURI(new URI(url));
    InputStream is = client.execute(request).getEntity().getContent();
    BufferedReader inb = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder("");
    String line;/* w  ww  .j  a v a2 s .co m*/
    String NL = System.getProperty("line.separator");
    while ((line = inb.readLine()) != null) {
        sb.append(line).append(NL);
    }
    inb.close();
    return sb.toString();
}

From source file:Main.java

public static HttpResponse getData(String url) {
    HttpResponse response = null;/*from w  w w .j a  va  2s .  com*/
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        response = client.execute(request);
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return response;
}

From source file:Main.java

/**
 * Performs an HTTP GET request to the specified url, and return the String
 * read from HttpResponse.//ww w.  j a v a2s  . co  m
 * @param url The web address to post the request to
 * @return The result of the request
 * @throws Exception
 */
public static String executeHttpGet(String url) throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(url));
        HttpResponse response = client.execute(request);
        in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();

        String result = sb.toString();
        return result;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:Main.java

public static String httpStringGet(String url, String enc) throws Exception {
    // This method for HttpConnection
    String page = "";
    BufferedReader bufferedReader = null;
    try {/*from   w  w  w.  j av a 2 s  .  c o m*/
        HttpClient client = new DefaultHttpClient();
        client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "android");

        HttpParams httpParams = client.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
        HttpConnectionParams.setSoTimeout(httpParams, 5000);

        HttpGet request = new HttpGet();
        request.setHeader("Content-Type", "text/plain; charset=utf-8");
        request.setURI(new URI(url));
        HttpResponse response = client.execute(request);
        bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), enc));

        StringBuffer stringBuffer = new StringBuffer("");
        String line = "";

        String NL = System.getProperty("line.separator");// "\n"
        while ((line = bufferedReader.readLine()) != null) {
            stringBuffer.append(line + NL);
        }
        bufferedReader.close();
        page = stringBuffer.toString();
        Log.i("page", page);
        System.out.println(page + "page");
        return page;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                Log.d("BBB", e.toString());
            }
        }
    }
}

From source file:com.github.yongchristophertang.engine.web.request.TestRequestBuilders.java

/**
 * Create a {@link HttpRequestBuilders} for a GET request.
 *
 * @param urlTemplate  a URL template; the resulting URL will be encoded
 * @param urlVariables zero or more URL variables
 *//*from w  ww .j  ava 2  s  .  co  m*/
public static HttpRequestBuilders get(String urlTemplate, Object... urlVariables) {
    return new HttpRequestBuilders(new HttpGet(), urlTemplate, "GET Request", urlVariables);
}

From source file:fr.digitbooks.android.examples.chapitre08.DigibooksRatingApi.java

public static String requestRatings(int count, String format) {
    String response = null;//from ww  w .  ja va 2  s.  c  o  m
    StringBuffer stringBuffer = new StringBuffer();
    BufferedReader bufferedReader = null;
    try {
        // Cration d'une requ?te de type GET
        HttpGet httpGet = new HttpGet();
        URI uri = new URI(Config.URL_SERVER + "data?count=" + count + "&format=" + format);
        httpGet.setURI(uri);
        // Cration d'une connexion
        AndroidHttpClient httpClient = AndroidHttpClient.newInstance("");
        HttpResponse httpResponse = httpClient.execute(httpGet);
        // Traitement de la rponse
        InputStream inputStream = httpResponse.getEntity().getContent();
        bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 1024);
        String readLine = bufferedReader.readLine();
        while (readLine != null) {
            stringBuffer.append(readLine);
            readLine = bufferedReader.readLine();
        }
        httpClient.close();
    } catch (Exception e) {
        Log.e(LOG_TAG, "IOException trying to execute request for " + e);
        return null;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                return null;
            }
        }
        response = stringBuffer.toString();
        if (Config.INFO_LOGS_ENABLED) {
            Log.i(LOG_TAG, "Reponse = " + response);
        }
    }

    return response;
}

From source file:eu.trentorise.smartcampus.portfolio.utils.NetUtility.java

public static Bitmap loadBitmapfromUrl(String imageUrl) {
    Bitmap resultImage = null;/*from   www.  j av a  2s  . co m*/
    HttpGet getRequest = new HttpGet();
    try {
        URI imageURI = new URI(imageUrl);
        getRequest.setURI(imageURI);
        HttpClient httpClient = HttpClientFactory.INSTANCE.getThreadSafeHttpClient();
        HttpResponse response = httpClient.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            Log.w(NetUtility.class.getName(), "Error: " + statusCode + " image url: " + imageUrl);
        } else {
            final HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream inputStream = null;
                try {
                    inputStream = new FlushedInputStream(entity.getContent());
                    resultImage = BitmapFactory.decodeStream(inputStream);
                } finally {
                    if (inputStream != null) {
                        inputStream.close();
                    }
                    entity.consumeContent();
                }
            }
        }
    } catch (Exception e) {
        getRequest.abort();
        e.printStackTrace();
        Log.w(NetUtility.class.getName(), "Error image url: " + imageUrl);
    }
    return resultImage;
}

From source file:inte.com.camel.cntrain.TrainReserveTest.java

@Test
public void test() {
    HttpGet httpGet = new HttpGet();
    fail("Not yet implemented");
}

From source file:com.tweetlanes.android.urlservice.ApiService.java

public static HttpResponse getRequest(String url, String debugName) {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    HttpResponse response = null;/*  w w  w.  j  a v  a 2  s  .  c  o  m*/
    try {
        request.setURI(new URI(url));
        //Log.d("tweetlanes url fetch", url);
        response = client.execute(request);
        //Log.d(TAG, debugName + " complete");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return response;
}

From source file:com.shafiq.mytwittle.urlservice.tweetmarker.TweetMarkerAPI.java

public static HttpResponse getRequest(String url, String debugName) {
    HttpClient client = new DefaultHttpClient();
    HttpGet request = new HttpGet();
    request.addHeader("X-Auth-Service-Provider", TwitterApi.TWITTER_VERIFY_CREDENTIALS_JSON);
    request.addHeader("X-Verify-Credentials-Authorization",
            TwitterManager.get().generateTwitterVerifyCredentialsAuthorizationHeader());
    HttpResponse response = null;/*from   w  w  w  .  j a  v  a 2  s .c  o m*/
    try {
        request.setURI(new URI(url));
        // Log.d("tweetlanes url fetch", url);
        response = client.execute(request);
        // Log.d(TAG, debugName + " complete");
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return response;
}