List of usage examples for org.apache.http.impl.client DefaultHttpClient DefaultHttpClient
public DefaultHttpClient()
From source file:com.alta189.cyborg.commandkit.util.HttpUtil.java
public static String readRandomLine(String url) { HttpGet request = new HttpGet(url); HttpClient httpClient = new DefaultHttpClient(); try {//from w w w .j a va 2 s .c o m HttpResponse response = httpClient.execute(request); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); List<String> lines = new ArrayList<String>(); String line = ""; while ((line = rd.readLine()) != null) { lines.add(line); } if (lines.size() > 1) { int i = randomNumber(0, lines.size() + 1); return lines.get(i); } else if (lines.size() == 1) { return lines.get(0); } } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:cn.newgxu.android.bbs.util.NewgxuUtils.java
public static final String get(String url, String charset) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); get.setHeader("Accept", "application/json"); HttpResponse response = null;//from w w w . j a v a 2s. co m HttpEntity entity = null; Scanner scanner = null; try { response = client.execute(get); entity = response.getEntity(); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream in = entity.getContent(); scanner = new Scanner(in, charset == null ? "utf-8" : charset).useDelimiter("\\A"); return scanner.hasNext() ? scanner.next() : null; } } catch (ClientProtocolException e) { Log.wtf(TAG, e); } catch (IOException e) { Log.wtf(TAG, e); } finally { try { if (entity != null) { entity.consumeContent(); } } catch (IOException e) { Log.wtf(TAG, e); } } return null; }
From source file:com.urucas.raddio.services.RequestTask.java
@Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response;//from ww w. j a v a 2 s . com String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); if (isCancelled()) { rsh.onError("Task cancel"); } } else { //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { rsh.onError(e.getMessage()); } catch (IOException e) { rsh.onError(e.getMessage()); } return responseString; }
From source file:com.drismo.logic.JsonFunctions.java
/** * Borrowed from:/*from ww w . jav a 2s .com*/ * http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/ * and slightly modified. * @param url the url to get JSON data from * @return the JSONObject containing all the info. */ public static JSONObject getJSONfromURL(String url) { //initialize InputStream is = null; String result = ""; JSONObject jArray = null; //http post try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(url); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch (Exception e) { Log.e("log_tag", "Error in http connection " + e.toString()); } //convert response to string try { BufferedReader reader = new BufferedReader(new InputStreamReader(is), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); result = sb.toString(); } catch (Exception e) { Log.e("log_tag", "Error converting result " + e.toString()); } //try parse the string to a JSON object try { jArray = new JSONObject(result); } catch (JSONException e) { Log.e("log_tag", "Error parsing data " + e.toString()); } return jArray; }
From source file:org.busko.locationtools.android.loggers.POSTLogger.java
POSTLogger() {
httpclient = new DefaultHttpClient();
}
From source file:brooklyn.networking.cloudstack.HttpUtil.java
public static HttpClient createHttpClient(URI uri, Optional<Credentials> credentials) { final DefaultHttpClient httpClient = new DefaultHttpClient(); // TODO if supplier returns null, we may wish to defer initialization until url available? if (uri != null && "https".equalsIgnoreCase(uri.getScheme())) { try {/*from ww w . j a va2 s .com*/ int port = (uri.getPort() >= 0) ? uri.getPort() : 443; SSLSocketFactory socketFactory = new SSLSocketFactory(new TrustAllStrategy(), SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme sch = new Scheme("https", port, socketFactory); httpClient.getConnectionManager().getSchemeRegistry().register(sch); } catch (Exception e) { LOG.warn("Error in HTTP Feed of {}, setting trust for uri {}", uri); throw Exceptions.propagate(e); } } // Set credentials if (uri != null && credentials.isPresent()) { String hostname = uri.getHost(); int port = uri.getPort(); httpClient.getCredentialsProvider().setCredentials(new AuthScope(hostname, port), credentials.get()); } return httpClient; }