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.seamusdawkins.rest.utils.HttpHelper.java

public static JSONArray doGetArray(Activity act, String url, String charset, String token)
        throws IOException, Exception, JSONException {
    HttpGet conn = new HttpGet(url);

    conn.addHeader(X_APPLICATION_TOKEN, token);
    conn.addHeader(CONNECTION, "close");
    conn.addHeader(CONTENT_TYPE, "application/x-www-form-urlencoded");

    JSONArray o = null;/*from w w w . j ava 2  s.co m*/
    StringBuilder sb = new StringBuilder();
    resp = null;
    try {
        HttpClient httpClient = new DefaultHttpClient();
        resp = httpClient.execute(conn);
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    try {
        if (resp != null) {

            status = resp.getStatusLine().getStatusCode();

            if (status == 200) {
                InputStream content = resp.getEntity().getContent();
                BufferedReader buffer = new BufferedReader(new InputStreamReader(content));

                String text;

                while ((text = buffer.readLine()) != null) {

                    sb.append(text);

                }
                o = new JSONArray(sb.toString());
            } else {
                JSONArray arr = new JSONArray();
                JSONObject obj = new JSONObject();
                obj.put("statuscode", String.valueOf(status));
                arr.put(0, obj);
                return arr;
            }
        }

    } catch (Exception e) {
        return null;
    }
    return o;

}

From source file:org.openinfinity.cloud.util.http.HttpHelper.java

public static String executeHttpRequest(HttpClient client, String url) {
    HttpUriRequest request = new HttpGet(url);

    try {/*from   w  w  w  .j a v a  2 s. c o  m*/
        HttpResponse response = client.execute(request);
        int status = response.getStatusLine().getStatusCode();
        if (status >= 200 && status < 300) {
            HttpEntity entity = response.getEntity();
            return entity != null ? EntityUtils.toString(entity) : null;
        }
    } catch (ClientProtocolException e) {
        LOG.warn(EXCEPTION_WHILE_EXECUTING_HTTP_REQUEST + "----" + e.getMessage());
    } catch (IOException e) {
        LOG.warn(EXCEPTION_WHILE_EXECUTING_HTTP_REQUEST + "----" + e.getMessage());
    } catch (Exception e) {
        LOG.warn(EXCEPTION_WHILE_EXECUTING_HTTP_REQUEST + "----" + e.getMessage());
    }
    return null;
}

From source file:Main.java

/**
 * Generate an input stream reading from an android URI
 * /*from  w  w  w.j  a  v a 2  s .  co  m*/
 * @param uri
 * @return
 * @throws IOException
 */
public static InputStream getFromURI(Context context, Uri uri) throws IOException {

    if (uri.getScheme().equals("content"))
        return context.getContentResolver().openInputStream(uri);
    else if (uri.getScheme().equals("file")) {
        URL url = new URL(uri.toString());

        return url.openStream();
    } else {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(uri.toString());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null)
            return entity.getContent();
        else
            throw new IOException("No HTTP response");
        // Use the regular java stuff
        // URL url = new URL(uri.toString());

        // return url.openStream();
    }
}

From source file:Main.java

public static String openGetConnection(String urlString, Map<String, String> map) {
    List<NameValuePair> nameValuePairs = new ArrayList<>();
    for (String key : map.keySet()) {
        nameValuePairs.add(new BasicNameValuePair(key, map.get(key)));
    }/*from   ww  w .  jav a 2 s .c  o m*/
    String params = URLEncodedUtils.format(nameValuePairs, "utf-8");
    urlString += "?";
    urlString += params;
    HttpClient client = new DefaultHttpClient();
    HttpGet get = new HttpGet(urlString);
    try {
        HttpResponse httpResponse = client.execute(get);
        BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
        StringBuilder json = new StringBuilder("");
        String line = null;
        while ((line = br.readLine()) != null) {
            json.append(line);
        }
        return json.toString();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.jboss.as.test.clustering.cluster.web.async.AsyncServletTestCase.java

private static void assertValue(HttpClient client, URI uri, int value) throws IOException {
    HttpResponse response = client.execute(new HttpGet(uri));
    try {//w w w. j  av  a 2  s  .  c  o m
        Assert.assertEquals(HttpServletResponse.SC_OK, response.getStatusLine().getStatusCode());
        Assert.assertEquals(value,
                Integer.parseInt(response.getFirstHeader(SimpleServlet.VALUE_HEADER).getValue()));
    } finally {
        HttpClientUtils.closeQuietly(response);
    }
}

From source file:Main.java

public static String GET(String url) {
    InputStream inputStream = null;
    String result = "";
    try {// w ww  .  j  a va 2s .c  o  m

        // create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // make GET request to the given URL
        HttpResponse httpResponse = httpclient.execute(new HttpGet(url));

        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if (inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
    }

    return result;
}

From source file:cloud4all.Utils.NetworkAnode.java

public static void sendRequest(String anode) {
    AsyncTask<String, Void, Void> at = new AsyncTask<String, Void, Void>() {
        @Override/* w  ww  .j a  va2  s.  c o  m*/
        protected Void doInBackground(String... url) {
            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet command = new HttpGet(url[0]);
                HttpResponse response = client.execute(command);

                InputStreamReader isr = new InputStreamReader(response.getEntity().getContent());
                BufferedReader in = new BufferedReader(isr);
                String res = in.readLine();
                Log.d("GPIIUserListeners", "GPII has returned: " + res);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    };
    at.execute(anode);
}

From source file:core.RESTCalls.RESTGet.java

public static InputStream httpGetResponse(String urlStr) {

    InputStream inputStream = null;

    try {/*from w ww  .j ava 2  s .c o  m*/

        HttpClient client = new DefaultHttpClient();

        HttpGet get = new HttpGet(urlStr);

        HttpResponse response = client.execute(get);

        inputStream = response.getEntity().getContent();
    }

    catch (Exception ex) {

        ex.printStackTrace();
    }

    return inputStream;
}

From source file:Main.java

public static Bitmap getGoogleMapThumbnail(double latitude, double longitude) {
    String URL = "http://maps.google.com/maps/api/staticmap?center=" + latitude + "," + longitude
            + "&zoom=15&size=600x600&sensor=false";

    Bitmap bmp = null;/*from ww  w .j  ava 2 s . c  om*/
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet request = new HttpGet(URL);

    InputStream in = null;
    try {
        in = httpclient.execute(request).getEntity().getContent();
        bmp = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IllegalStateException 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 bmp;
}

From source file:ee.ioc.phon.netspeechapi.Utils.java

/**
 * <p>Executes the given HTTP request using the given HTTP client,
 * and returns the received entity as string.
 * Returns <code>null</code> if the query was performed (i.e. the server
 * was reachable) but resulted in a failure,
 * e.g. 404 error.</p>//  www  .  ja v a 2s  .c o m
 * 
 * @param client HTTP client
 * @param request HTTP request (e.g. GET or POST)
 * @return response as String
 * @throws IOException
 */
public static String getResponseEntityAsString(HttpClient client, HttpUriRequest request) throws IOException {
    try {
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();

        if (entity == null) {
            return null;
        }
        int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            return null;
        }
        if (entity.getContentEncoding() == null) {
            return EntityUtils.toString(entity, HTTP.UTF_8);
        }
        return EntityUtils.toString(entity);
    } finally {
        client.getConnectionManager().shutdown();
    }
}