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.codingrhemes.steamsalesmobile.HttpThumbnails.java

public static Bitmap readPictureFromTheWeb(String URL) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(URL);
    Bitmap thePicture = null;//from  w ww  .  ja  v a  2 s.co m

    try {

        HttpResponse response = httpClient.execute(httpGet);
        StatusLine statusLine = response.getStatusLine();
        int statusCode = statusLine.getStatusCode();
        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream inputStream = entity.getContent();
            thePicture = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } else {
            Log.d("JSON", "Failed to download file");
        }
    } catch (Exception e) {
        Log.d("HttpThumbnails", e.getLocalizedMessage());
    }
    return thePicture;
}

From source file:com.lee.sdk.utils.NetUtils.java

/**
 * ?get?/*from   ww  w .  j a  va 2 s .  c om*/
 * 
 * @param url url
 * @return string
 * @throws IOException IOException
 * @throws ClientProtocolException ClientProtocolException
 */
public static String getStringFromUrl(String url) throws ClientProtocolException, IOException {
    HttpGet get;
    try {
        get = new HttpGet(url);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        return null;
    }
    HttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute(get);
    if (response != null) {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            return EntityUtils.toString(entity, "UTF-8");
        }
    }
    return null;
}

From source file:com.mmd.mssp.util.HttpUtil.java

public static String httpGet(String uri) throws IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(uri);
    HttpResponse response = httpclient.execute(httpget);

    logger.info(httpget.getURI().toString() + " == " + response.getStatusLine());

    HttpEntity entity = response.getEntity();
    Header[] cts = response.getHeaders("Content-Type");
    String charset = "UTF-8";
    if (cts.length > 0) {
        String ContentType = cts[0].getValue();
        if (ContentType.contains("charset")) {
            charset = ContentType.split("charset=")[1];
        }/*  ww  w .  j  a  v  a 2s  .  com*/
    }
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            StringBuffer buffer = new StringBuffer();
            char[] chars = new char[BUFFER_SIZE];
            while (true) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(instream, charset));
                int len = reader.read(chars);
                if (len < 0) {
                    break;
                }
                buffer.append(chars, 0, len);
            }
            return buffer.toString();
        } catch (IOException ex) {
            throw ex;
        } catch (RuntimeException ex) {
            httpget.abort();
            throw ex;
        } finally {
            instream.close();
            httpclient.getConnectionManager().shutdown();
        }
    }
    return "";
}

From source file:Extras.JSON.java

public static HttpResponse request(String URL) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(URL);
    HttpResponse response = null;//from  w w  w. j  a v a2 s.c  o  m
    try {
        response = httpclient.execute(httppost);
    } catch (Exception e) {
    }
    return response;
}

From source file:org.ojbc.util.helper.HttpUtils.java

/**
 * Send the specified payload to the specified http endpoint via POST.
 * @param payload//  w w  w . j  a v  a  2s  . c om
 * @param url
 * @return the http response
 * @throws Exception
 */
public static String post(String payload, String url) throws Exception {
    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(url);
    post.setEntity(new StringEntity(payload, Consts.UTF_8));
    HttpResponse response = client.execute(post);
    HttpEntity reply = response.getEntity();
    StringWriter sw = new StringWriter();
    BufferedReader isr = new BufferedReader(new InputStreamReader(reply.getContent()));
    String line;
    while ((line = isr.readLine()) != null) {
        sw.append(line);
    }
    sw.close();
    return sw.toString();
}

From source file:siarhei.luskanau.gps.tracker.free.service.sync.tracking.json.SendJsonForm.java

private static InputStream requestInputStream(HttpUriRequest httpUriRequest) throws Exception {
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setSoTimeout(httpParams, CONNECTION_TIMEOUT);
    HttpConnectionParams.setConnectionTimeout(httpParams, CONNECTION_TIMEOUT);

    HttpClient httpClient = new DefaultHttpClient(httpParams);
    HttpResponse httpResponse = httpClient.execute(httpUriRequest);
    int statusCode = httpResponse.getStatusLine().getStatusCode();
    if (statusCode != 200 && statusCode != 201) {
        String responseString = null;
        try {/*from   w w w .ja  v  a  2s .  c  om*/
            responseString = read(httpResponse.getEntity().getContent());
        } catch (Throwable t) {
            responseString = null;
        }
        throw new HttpResponseException(statusCode,
                statusCode + " " + httpResponse.getStatusLine().getReasonPhrase() + "\n" + responseString);
    }
    return httpResponse.getEntity().getContent();
}

From source file:org.rexify.eclipse.helper.InternetHelper.java

static public String GET(String url) {
    HttpClient client = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);

    try {//w  w w  .java 2  s  . co  m
        HttpResponse resp = client.execute(httpget);
        HttpEntity entity = resp.getEntity();

        if (entity != null) {
            InputStream content_stream = entity.getContent();
            return InternetHelper.convertStreamToString(content_stream);
        } else {
            // Use a sane fallback
            return "[{\"name\": \"Empty Skeleton\", \"template\": \"# Rexfile\nuse Rex -base;\n\"}]";
        }
    } catch (ClientProtocolException pro_ex) {
        // display error message
        System.out.println(pro_ex);
    } catch (IOException io_ex) {
        // display error message
        System.out.println(io_ex);
    }

    // Use a sane fallback
    return "[{\"name\": \"Empty Skeleton\", \"template\": \"# Rexfile\nuse Rex -base;\n\"}]";
}

From source file:com.byteridge.bookcircle.framework.listener.ResponseParser.java

public static User GetAuthorizedUser() throws Exception {
    HttpGet getRequest = new HttpGet("http://www.goodreads.com/api/auth_user");
    _Consumer.sign(getRequest);//from   w  ww .  j  av a 2  s .  c  om
    HttpClient httpClient = new DefaultHttpClient();
    HttpResponse response = httpClient.execute(getRequest);

    Response responseData = ResponseParser.parse(response.getEntity().getContent());
    return responseData.get_User();
}

From source file:Main.java

/**
 * Performs an HTTP GET request to the specified url, and return the String
 * read from HttpResponse.//from  w  w w. j a v  a 2  s.c  o  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:at.uni_salzburg.cs.ckgroup.pilot.sensor.OpenStreetMapTileCache.java

public static void downloadFile(String url, File file) throws IOException {

    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(url);
    HttpResponse response;//from w  ww .  j a va  2s  . com

    response = httpclient.execute(httpget);
    FileOutputStream outStream = new FileOutputStream(file);

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