Example usage for org.apache.http.client HttpClient execute

List of usage examples for org.apache.http.client HttpClient execute

Introduction

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

Prototype

HttpResponse execute(HttpUriRequest request) throws IOException, ClientProtocolException;

Source Link

Document

Executes HTTP request using the default context.

Usage

From source file:com.fdesousa.android.WheresMyTrain.Library.json.TflJsonFetcher.java

/**
 * Utility method for fetching an InputStream from a designated URI
 * and returning it for processing.<br/>
 * Static method to aide in easy, wide-spread use
 * @param uri - the URI of the data to fetch
 * @return InputStream containing the response of the request
 * @throws IllegalStateException - in case of a problem, or if connection was aborted
 * @throws IOException - if the stream could not be created
 *///from ww  w .jav  a2  s  .  c o m
public static InputStream fetchNewJson(URI uri) throws IllegalStateException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(uri);
    HttpResponse httpresponse = httpclient.execute(httppost);
    HttpEntity entity = httpresponse.getEntity();

    return entity.getContent();
}

From source file:com.foobnix.util.LyricUtil.java

public static String fetchLyric(String artist, String title) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("artist", artist));
    params.add(new BasicNameValuePair("song", title));
    params.add(new BasicNameValuePair("fmt", "json"));

    String paramsList = URLEncodedUtils.format(params, "UTF-8");
    HttpGet request = new HttpGet("http://lyricwiki.org/api.php?" + paramsList);
    HttpClient client = new DefaultHttpClient();

    try {//w  w  w. java 2  s.c om
        HttpResponse execute = client.execute(request);
        String response = EntityUtils.toString(execute.getEntity());

        LOG.d(response);

        if (response != null && response.indexOf('{') > 0) {
            JSONObject obj = new JSONObject(response.substring(response.indexOf('{')));
            return obj.getString("lyrics");
        }
    } catch (ClientProtocolException e) {
        LOG.e("", e);
    } catch (IOException e) {
        LOG.e("", e);
    } catch (JSONException e) {
        LOG.e("", e);
    }

    return "Lyric not found";
}

From source file:com.precioustech.fxtrading.oanda.restapi.OandaTestUtils.java

public static final void mockHttpInteraction(String fname, HttpClient mockHttpClient) throws Exception {
    CloseableHttpResponse mockResp = mock(CloseableHttpResponse.class);
    when(mockHttpClient.execute(any(HttpUriRequest.class))).thenReturn(mockResp);

    HttpEntity mockEntity = mock(HttpEntity.class);

    when(mockResp.getEntity()).thenReturn(mockEntity);

    StatusLine mockStatusLine = mock(StatusLine.class);

    when(mockResp.getStatusLine()).thenReturn(mockStatusLine);
    when(mockStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
    when(mockEntity.getContent()).thenReturn(new FileInputStream(fname));
}

From source file:tech.sirwellington.alchemy.http.VerbAssertions.java

private static void assertRequestWith(HttpVerb verb, Class<? extends HttpUriRequest> type) throws Exception {
    HttpClient mockClient = mock(HttpClient.class);
    when(mockClient.execute(any(HttpUriRequest.class))).thenReturn(createFakeApacheResponse());

    URI uri = createFakeUri();// w  ww  .  j a v a2s.co  m

    HttpRequest request = HttpRequest.Builder.newInstance().usingUrl(uri.toURL()).build();

    verb.execute(mockClient, request);

    ArgumentCaptor<HttpUriRequest> captor = forClass(HttpUriRequest.class);

    verify(mockClient).execute(captor.capture());

    HttpUriRequest requestMade = captor.getValue();

    assertThat(requestMade, notNullValue());
    assertThat(requestMade.getURI(), is(uri));
    assertThat(requestMade.getClass(), sameInstance(type));
}

From source file:com.hackathon.gavin.string.parser.MyAccountParser.java

public static JSONArray httpGetCall(String urlString, String jsonArrayName) {
    JSONArray result = null;/*w w w  . j  a  v  a2 s. c  o m*/
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(urlString);

    try {
        HttpResponse getResponse = client.execute(request);
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(getResponse.getEntity().getContent(), "UTF-8"));
        StringBuilder builder = new StringBuilder();
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        //result = new JSONObject(builder.toString()).getJSONArray(jsonArrayName);
        JSONObject obj = new JSONObject(builder.toString());
        result = obj.getJSONArray(jsonArrayName);
    } catch (Exception e) {
        System.out.println(e.toString());
    }
    return result;
}

From source file:fr.shywim.antoinedaniel.utils.GcmUtils.java

public static void sendRegistrationIdToBackend(Context context, String regid, int version) {
    try {/*from www.j  av a 2 s  .c  o  m*/
        HttpPost post = new HttpPost(context.getString(R.string.api_register));
        List<NameValuePair> params = new ArrayList<>(2);
        params.add(new BasicNameValuePair("regid", regid));
        params.add(new BasicNameValuePair("version", Integer.toString(version)));
        post.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(post);

        int responseCode = response.getStatusLine().getStatusCode();
        if (responseCode == 200 || responseCode == 201) {
            context.getSharedPreferences(AppConstants.GOOGLE_PREFS, Context.MODE_PRIVATE).edit()
                    .putBoolean(AppConstants.GOOGLE_GCM_REGISTERED, true).apply();
        }
    } catch (IOException e) {
        Crashlytics.logException(e);
    }
}

From source file:kr.pe.javarss.mybus.task.GBusPageParser.java

public static InputStream getPageInputStream(String url) throws IOException {

    //    :   30   ?
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, CONNECTION_TIMEOUT);

    HttpClient hc = new DefaultHttpClient(params);
    HttpResponse res = hc.execute(new HttpGet(url));
    if (res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
        throw new UnknownServiceException();
    }/* w ww  . ja  va2s  .  c o m*/

    return res.getEntity().getContent();
}

From source file:appserver.grupo5.http.java

private static Response executeRequest(HttpClient client, HttpUriRequest request) throws Exception {
    HttpResponse response = client.execute(request);
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    StringBuilder result = new StringBuilder();
    String line;/*from w  ww. j av a  2  s.  co  m*/
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }
    String entity = result.toString();
    int status = response.getStatusLine().getStatusCode();
    String contentType = response.getFirstHeader("Content-type").getValue();
    return Response.status(status).entity(entity).header("Content-type", contentType).build();
}

From source file:com.buddycloud.friendfinder.HttpUtils.java

public static JsonElement consumeJSON(String URL) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    HttpResponse httpResponse = client.execute(httpGet);

    JsonElement parse = new JsonParser()
            .parse(new JsonReader(new InputStreamReader(httpResponse.getEntity().getContent())));

    return parse;
}

From source file:Main.java

public static HttpResponse getData(String url) {
    HttpResponse response = null;/* w  ww . j a v a2  s .c o m*/
    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;
}