Cómo realizar una conexión http get en Java
En Java, disponemos de la clase java.net.URL, una clase de alto nivel que nos permite trabajar de una forma sencilla con las Uniform Resource Locator, es decir, URL.
Para realizar una conexión HTTP GET, podemos usar el método openStream (de URL) para obtener un clásico objeto InputStream, el resto es simplemente leer el Stream.
El siguiente ejemplo, realiza un HTTP GET a la URL “http://www.emol.com” para guardar el contenido en un String e imprimir el resultado.
package org.deerme.examples; import java.io.*; import java.net.*; /** * It's just an example an HTTP GET * @author deerme.org */ public class ExampleHTTPGet { private String siteContent = ""; public ExampleHTTPGet( String strUrl ) { String content; try { byte[] buffer = new byte[4096]; URL url = new URL( strUrl ); BufferedInputStream bis = new BufferedInputStream(url.openStream()); int bytesRead = 0; while ((bytesRead = bis.read(buffer)) != -1) { content = new String(buffer, 0, bytesRead); this.siteContent = this.siteContent + content; } bis.close(); System.out.println( this.siteContent ); } catch (Exception ex) { System.out.println("OMG, Houston, we have a exception " + ex.getMessage() + " xD"); } } public static void main(String[] args) { // In my case, I use a proxy System.setProperty("http.proxyHost", "localhost"); System.setProperty("http.proxyPort", "44444"); new ExampleHTTPGet("http://www.emol.com"); } }
Si no sales a través de un Proxy, debemes comentar las líneas System.setProperty,
hola buenas,
estoy intentando hacer una aplicacion para android y nesesito que haga un http de este url http://130.206.85.125/poi_dp/radial_search?lat=1&lon=1&category=test_poi pero siempre me esta dando nulo todo.
he provado tu codigo de arriba y no se por que falla