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:org.prx.playerhater.util.PlaylistParser.java

public static Uri[] parsePlaylist(Uri uri) {
    try {//from   w  w  w.  j a  v  a  2 s. com
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpHead(uri.toString()));
        Header contentType = response.getEntity().getContentType();
        if (contentType != null) {
            String mimeType = contentType.getValue().split(";")[0].trim();

            for (String plsMimeType : PLS_MIME_TYPES) {
                if (plsMimeType.equalsIgnoreCase(mimeType)) {
                    return parsePls(uri);
                }
            }

            for (String m3uMimeType : M3U_MIME_TYPES) {
                if (m3uMimeType.equalsIgnoreCase(mimeType)) {
                    return parseM3u(uri);
                }
            }
        }
    } catch (Exception e) {
    }
    return new Uri[] { uri };
}

From source file:org.thiesen.osm.pt.ConverterMain.java

private static InputStream getOSMInputStream(final String source) throws ClientProtocolException, IOException {
    final HttpClient client = new DefaultHttpClient();

    final HttpGet get = new HttpGet(source);

    final HttpResponse response = client.execute(get);

    final HttpEntity entity = response.getEntity();

    if (entity != null) {
        final InputStream content = entity.getContent();

        if (content != null) {

            content.read(new byte[2], 0, 2); // Skip first two bytes, because they are invalid

            final CBZip2InputStream bzipStream = new CBZip2InputStream(new BufferedInputStream(content));

            return bzipStream;
        }// w w w  . jav a  2 s.  c om
    }

    return null;
}

From source file:org.npr.android.news.DownloadDrawable.java

public static Drawable createFromUrl(String url) {
    InputStream data = null;//from   ww w  .  j  ava 2  s.c o  m
    Log.d(LOG_TAG, "Starting download");
    HttpClient http = new DefaultHttpClient();
    HttpGet method = new HttpGet(url);

    HttpResponse response = null;
    try {
        response = http.execute(method);
        data = response.getEntity().getContent();
    } catch (ClientProtocolException e) {
        Log.e(LOG_TAG, "error downloading", e);
    } catch (IOException e) {
        Log.e(LOG_TAG, "error downloading", e);
    } catch (IllegalStateException e) {
        Log.e(LOG_TAG, "error downloading", e);
    }
    Log.d(LOG_TAG, "Download complete");
    return Drawable.createFromStream(data, url);
}

From source file:com.alta189.cyborg.commandkit.util.HttpUtil.java

public static String readURL(String url) {
    HttpGet request = new HttpGet(url);
    HttpClient httpClient = new DefaultHttpClient();
    try {//from   w  w  w .jav  a 2 s . c  o m
        HttpResponse response = httpClient.execute(request);
        return EntityUtils.toString(response.getEntity());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:org.prx.playerhater.util.PlaylistParser.java

private static Uri[] parseM3u(Uri uri) {

    try {/* w  w w .  j  ava2  s  .c o  m*/
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(uri.toString()));
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line;
        ArrayList<Uri> uriList = new ArrayList<Uri>();
        do {
            line = reader.readLine();
            if (line != null) {
                if (!line.startsWith("#")) {
                    uriList.add(Uri.parse(line.trim()));
                }
            }
        } while (line != null);
        if (uriList.size() > 0) {
            Uri[] res = new Uri[uriList.size()];
            return uriList.toArray(res);
        }
    } catch (Exception e) {

    }
    return new Uri[] { uri };
}

From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromGoogleMapsApi.java

public static String getAddress(double latitude, double longitude) {
    String strAddress = "";
    String point = latitude + "," + longitude;
    String url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude
            + "&sensor=true&language=en";

    HttpPost httppost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();
    String result = "";
    try {/*from   w  w w .j a va2s .  com*/
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntityGet = response.getEntity();
        if (resEntityGet != null) {
            result = EntityUtils.toString(resEntityGet);
        }
    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
    try {
        /*Keeps the first address form the results*/
        if (result != null) {
            JSONObject json = new JSONObject(result);
            JSONArray ja = json.getJSONArray("results");
            //System.out.println(result.toString());
            for (int i = 0; i < 1; i++) {
                strAddress = ja.getJSONObject(i).getString("formatted_address");
            }
        }
    } catch (Exception e) {
    }
    try {
        if (!strAddress.equals("")) {
            pointAddressMap.put(point, strAddress);
            FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true);
        }
    } catch (IOException ex) {
        Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return strAddress;
}

From source file:org.ow2.bonita.facade.rest.apachehttpclient.ApacheHttpClientUtil.java

public static HttpResponse executeHttpConnection(String uri, byte[] content, String optionsHeader,
        String username, String password)
        throws URISyntaxException, IOException, IOException, ClassNotFoundException {
    String serverAddress = HttpRESTUtil.getRESTServerAddress();
    HttpPost post = new HttpPost(serverAddress + uri);

    ByteArrayEntity entity = new ByteArrayEntity(content);
    entity.setContentType("application/octet-stream");
    post.setHeader("options", optionsHeader);
    post.setEntity(entity);/*  w w w  .  j a  v  a2s.  c  om*/

    HttpClient client = ApacheHttpClientUtil.getHttpClient(serverAddress, username, password);
    HttpResponse httpresponse = client.execute(post);
    return httpresponse;
}

From source file:org.prx.playerhater.util.PlaylistParser.java

private static Uri[] parsePls(Uri uri) {
    try {/*from   w  w w .  jav  a 2  s .co  m*/
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = httpclient.execute(new HttpGet(uri.toString()));
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String header = reader.readLine();
        if (header.trim().equalsIgnoreCase("[playlist]")) {
            String line;
            ArrayList<Uri> uriList = new ArrayList<Uri>();
            do {
                line = reader.readLine();
                if (line != null) {
                    if (line.startsWith("File")) {
                        String fileName = line.substring(line.indexOf("=") + 1).trim();
                        uriList.add(Uri.parse(fileName));
                    }
                }
            } while (line != null);
            if (uriList.size() > 0) {
                Uri[] res = new Uri[uriList.size()];
                return uriList.toArray(res);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return new Uri[] { uri };
}

From source file:com.android.dialer.lookup.LookupUtils.java

public static String httpGet(HttpGet request) throws IOException {
    HttpClient client = new DefaultHttpClient();

    request.setHeader("User-Agent", USER_AGENT);

    HttpResponse response = client.execute(request);
    int status = response.getStatusLine().getStatusCode();

    // Android's org.apache.http doesn't have the RedirectStrategy class
    if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
        Header[] headers = response.getHeaders("Location");

        if (headers != null && headers.length != 0) {
            HttpGet newGet = new HttpGet(headers[headers.length - 1].getValue());
            for (Header header : request.getAllHeaders()) {
                newGet.addHeader(header);
            }//from w w w.  ja v a  2s . c om
            return httpGet(newGet);
        } else {
            throw new IOException("Empty redirection header");
        }
    }

    if (status != HttpStatus.SC_OK) {
        throw new IOException("HTTP failure (status " + status + ")");
    }

    return EntityUtils.toString(response.getEntity());
}

From source file:org.hobbit.spatiotemporalbenchmark.transformations.value.AddressFromFoursquareApi.java

public static String getAddress(double latitude, double longitude) {
    String strAddress = "";
    String point = latitude + "," + longitude;

    String url = "https://api.foursquare.com/v2/venues/search?ll=" + latitude + "," + longitude
            + "&oauth_token=5TJR4WQZSOW0ZWTE4ENMXKO3Y415252GITEMRPQIVPMEGCYK&v=20120723&limit=1&accept-language=en";

    HttpPost httppost = new HttpPost(url);
    HttpClient httpclient = new DefaultHttpClient();
    String result = "";
    try {/*from w  ww  .j  a  v a 2 s . c om*/
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntityGet = response.getEntity();
        if (resEntityGet != null) {
            result = EntityUtils.toString(resEntityGet);
        }
    } catch (Exception e) {
        System.out.println("Exception:" + e);
    }
    try {
        /*Keeps the first address form the results*/
        if (result != null) {
            JSONObject json = new JSONObject(result);
            JSONObject jo = json.getJSONObject("response");
            JSONArray ja = jo.getJSONArray("venues");
            for (int i = 0; i < 1; i++) {
                strAddress = ja.getJSONObject(i).getJSONObject("location").getJSONArray("formattedAddress")
                        .toString();
                strAddress = strAddress.replace("},{", ",").replace("[", "").replace("]", "").replace("\"", "");
            }
        }
    } catch (Exception e) {
    }
    try {
        if (!strAddress.equals("")) {
            pointAddressMap.put(point, strAddress);
            FileUtils.writeStringToFile(f, point + "=" + strAddress + "\n", true);
        }
    } catch (IOException ex) {
        Logger.getLogger(CoordinatesToAddress.class.getName()).log(Level.SEVERE, null, ex);
    }
    return strAddress;
}