Example usage for org.apache.http.impl.client BasicResponseHandler BasicResponseHandler

List of usage examples for org.apache.http.impl.client BasicResponseHandler BasicResponseHandler

Introduction

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

Prototype

BasicResponseHandler

Source Link

Usage

From source file:nl.coralic.beta.sms.utils.http.HttpHandler.java

private static Response doPost(String url, List<NameValuePair> postdata) {
    HttpClient httpclient = getHttpClient();
    try {//  w w  w .j ava2s  .  c  o m
        HttpPost httpost = new HttpPost(url);
        httpost.setEntity(new UrlEncodedFormEntity(postdata, HTTP.UTF_8));
        HttpResponse response = httpclient.execute(httpost);
        if (response.getStatusLine().getStatusCode() == 200) {
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            return new Response(responseHandler.handleResponse(response));
        } else {
            return new Response(R.string.ERR_PROV_NO_RESP);
        }
    } catch (Exception e) {
        return new Response(R.string.ERR_CONN_ERR);
    } finally {
        // Release the resources
        httpclient.getConnectionManager().shutdown();
    }
}

From source file:org.epop.dataprovider.googlescholar.GoogleScholarGetterFromId.java

static List<Literature> getFromId(String userId) {
    // http://scholar.google.it/citations?user=q21xxm4AAAAJ&pagesize=100
    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("user", userId));
    qparams.add(new BasicNameValuePair("pagesize", "100"));

    URI uri;/* w  w w  .j av  a  2s .  co  m*/
    String responseBody = null;
    try {
        uri = URIUtils.createURI("http", GOOGLE_SCHOLAR, -1, "", URLEncodedUtils.format(qparams, "UTF-8"),
                null);
        uri = new URI(uri.toString().replace("citations/?", "citations?"));
        HttpGet httpget = new HttpGet(uri);
        System.out.println(httpget.getURI());
        HttpClient httpclient = new DefaultHttpClient();
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseBody = httpclient.execute(httpget, responseHandler);
        //System.out.println(responseBody);
        int counter = 1;
        String newResponseBody = responseBody;
        while (newResponseBody.contains("class=\"cit-dark-link\">Next &gt;</a>")) {
            URI newUri = new URI(uri.toString() + "&cstart=" + counter * 100);
            httpget = new HttpGet(newUri);
            System.out.println(httpget.getURI());
            httpclient = new DefaultHttpClient();
            newResponseBody = httpclient.execute(httpget, responseHandler);
            //System.out.println(newResponseBody);
            responseBody = responseBody + newResponseBody;
            counter++;
        }
    } 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 the result as string
    return parsePage(new StringReader(responseBody));
}

From source file:com.cas.server.TokenHttpRequest.java

public String doHTTPRequest(String url) {
    String responseBody = "";
    String token = "";

    DefaultHttpClient httpClient = new DefaultHttpClient();

    // Creating HTTP Post 
    HttpGet httpPost = new HttpGet(url);

    try {//from ww  w  . j a  v a 2s  .co m
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        responseBody = httpClient.execute(httpPost, responseHandler);

        JSONObject jObject = new JSONObject(responseBody);
        token = jObject.getString("token");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // writing exception to log 
        e.printStackTrace();
    } catch (IOException e) {
        // writing exception to log 
        e.printStackTrace();
    }

    return token;
}

From source file:com.uc.dca.util.HttpHandler.java

public String get(String request) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(request);
    Log.d(TAG, "executing request " + httpGet.getURI());
    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpClient.execute(httpGet, responseHandler);
    //        Log.d(TAG, responseBody);
    return responseBody;
}

From source file:edu.vt.vbi.patric.cache.ENewsGenerator.java

public boolean createCacheFile(String filePath) {

    boolean isSuccess = false;

    try (CloseableHttpClient client = HttpClientBuilder.create().build()) {

        HttpGet httpRequest = new HttpGet(sourceURL);
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String strResponseBody = client.execute(httpRequest, responseHandler);

        if (strResponseBody.length() > 0) {
            PrintWriter enewsOut = new PrintWriter(new FileWriter(filePath));
            enewsOut.println(strResponseBody);
            enewsOut.close();//w ww .j a  va2s. co m
        }
        isSuccess = true;
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return isSuccess;
}

From source file:io.werval.maven.StartMojoIT.java

@Test
public void startMojoIntegrationTest() throws IOException, InterruptedException {
    final Holder<Exception> errorHolder = new Holder<>();
    Thread runThread = new Thread(newRunnable(errorHolder, "werval:start"), "maven-werval-start-thread");
    try {// ww w  .j  a v  a2 s  .  com
        runThread.start();

        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet("http://localhost:23023/");
        ResponseHandler<String> handler = new BasicResponseHandler();

        await().atMost(60, SECONDS).pollDelay(5, SECONDS).until(() -> {
            try {
                return client.execute(get, handler);
            } catch (Exception ex) {
                return null;
            }
        }, containsString("I ran!"));

        client.getConnectionManager().shutdown();
    } finally {
        runThread.interrupt();
    }
}

From source file:hotandcold.HNCAPI.java

public List<element> list() {
    String request = servIP + "verb=l";

    HttpClient client = new DefaultHttpClient();

    HttpGet get = new HttpGet(request);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    try {/*from w w  w  . j  a  va 2 s .c om*/
        String responseBody = client.execute(get, responseHandler);
        List<element> list = new LinkedList<>();

        JSONArray obj = new JSONArray(responseBody);

        for (int i = 0; i < obj.length(); i++) {

            JSONObject o = obj.getJSONObject(i);

            JSONObject t = o.getJSONObject("coord");

            coord c = new coord(t.getDouble("long"), t.getDouble("lat"));

            element e = new element(c, o.getString("user"), i);
            list.add(e);
        }

        return list;

    } catch (IOException | JSONException ex) {
        Logger.getLogger(HNCAPI.class.getName()).log(Level.SEVERE, null, ex);
    }

    return null;
}

From source file:gov.nih.nci.cagrid.portal.portlet.workflow.util.Utils.java

/**
 * Download a file//from ww w  . j  a v a  2s.  com
 * @param uri address of file to download
 * @return file contents
 * @throws IOException 
 */
public byte[] download(String uri) throws IOException {
    log.debug("Downloading File: " + uri);
    HttpGet get = new HttpGet(uri);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String body = http.execute(get, responseHandler);
    return body.getBytes();
}

From source file:org.thiesen.hhpt.geolookup.appengine.AppEngineStationFinder.java

public Stations makeGeoLookup(final double lat, final double lon, final double defaultSearchRadiusMiles) {
    final HttpClient client = new DefaultHttpClient();
    final HttpGet get = new HttpGet("http://hhpt-search.appspot.com/search?lat=" + lat + "&lng=" + lon
            + "&radius=" + defaultSearchRadiusMiles);

    try {//  www  .  j  ava  2  s. c  o m
        final ResponseHandler<String> responseHandler = new BasicResponseHandler();

        final String body = client.execute(get, responseHandler);

        return parse(body);

    } catch (final ClientProtocolException e) {
        e.printStackTrace();

    } catch (final IOException e) {
        e.printStackTrace();

    } catch (final JSONException e) {
        e.printStackTrace();
    }

    return new Stations();

}

From source file:uk.ac.jorum.integration.RestApiBaseTest.java

protected String makeRequest(String endpoint, String queryString) throws Exception {
    URI uri = URIUtils.createURI(apiProtocol, apiHost, jettyPort(), jettyPath() + endpoint, queryString, null);
    HttpGet httpget = new HttpGet(uri);
    httpget.addHeader("Accept", "application/json");
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    return client.execute(httpget, responseHandler);
}