List of usage examples for org.apache.http.client.methods HttpGet HttpGet
public HttpGet(final String uri)
From source file:fridgegameinstaller.MCJsonConf.java
public static void getJson(String path, int mb) throws IOException { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://api.fridgegame.com/mcconf/Fridgegame.json"); CloseableHttpResponse response1 = httpclient.execute(httpGet); try {/*from www. j a v a 2 s . c om*/ System.out.println(httpGet.toString()); System.out.println(response1.getStatusLine()); BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent())); String a; String output = ""; while ((a = br.readLine()) != null) { output += a + "\n"; } System.out.println(output); try { JSONObject json = new JSONObject(output); String mcArgs = json.getString("minecraftArguments"); String regex = "(-Xmx[^ ]*)"; Pattern p = Pattern.compile(regex); Matcher m = p.matcher(mcArgs); String newArgs = m.replaceAll("-Xmx" + mb + "M"); json.put("minecraftArguments", newArgs); FileWriter file = new FileWriter(path); try { file.write(json.toString()); } catch (IOException e) { e.printStackTrace(); } finally { file.flush(); file.close(); } } catch (JSONException e) { } } finally { response1.close(); } }
From source file:com.weavers.duqhan.util.CurrencyConverter.java
public static Double convert(String currencyFrom, String currencyTo) throws IOException { HttpClient httpclient = new DefaultHttpClient(); // HttpGet httpGet = new HttpGet("https://finance.yahoo.com/webservice/v1/symbols/allcurrencies/quote?format=json"); HttpGet httpGet = new HttpGet("https://cdn.shopify.com/s/javascripts/currencies.js"); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httpGet, responseHandler); httpclient.getConnectionManager().shutdown(); String rates = responseBody.split("rates:")[1].split("convert")[0]; rates = rates.substring(0, rates.length() - 1); ObjectMapper mapper = new ObjectMapper(); CurrencyRates jSONReader = null;/*from w ww . j a va 2 s .c o m*/ jSONReader = mapper.readValue(rates, CurrencyRates.class); // System.out.println("ssssssssssss == " + jSONReader.getINR()); double ratio = 0.0; if (currencyTo.equals("USD")) { ratio = jSONReader.getINR() / jSONReader.getUSD(); } else { ratio = jSONReader.getUSD() / jSONReader.getINR(); } return ratio; }
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))); }/* w w w . ja v 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:com.phonemetra.turbo.lockclock.weather.HttpRetriever.java
public static String retrieve(String url) { HttpGet request = new HttpGet(url); try {//from w w w . jav a 2 s . com HttpResponse response = new DefaultHttpClient().execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity); } } catch (IOException e) { } return null; }
From source file:Main.java
/** * Generate an input stream reading from an android URI * // ww w .ja v a2 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:pingdesktop.Model.HttpHandler.java
public static String doGet(String page_url) throws IOException { /* create the HTTP client and GET request */ HttpGet httpGet = new HttpGet(BaseUrl + page_url); /* execute request */ HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); /* process response */ if (httpResponse.getStatusLine().getStatusCode() == 200) { String responseText = EntityUtils.toString(httpEntity); return responseText; } else {/* w w w . j a v a 2 s . com*/ return "Invalid HTTP response: " + httpResponse.getStatusLine().getStatusCode(); } }
From source file:com.atomiton.watermanagement.ngo.util.HttpRequestResponseHandler.java
public static String sendGet(String serverURL, String qParams) throws Exception { String url = serverURL + "?" + qParams; CloseableHttpClient client = HttpClients.createDefault(); HttpGet request = new HttpGet(url); HttpResponse response = client.execute(request); // System.out.println("\nSending 'GET' request to URL : " + url); BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line);//www . j a v a 2s . com } client.close(); return result.toString(); }
From source file:it.polimi.brusamentoceruti.moviebookrest.boundary.JsonRequest.java
public static JSONObject doQuery(String Url) throws JSONException, IOException { String responseBody = null;/* w w w . j ava 2s .c o m*/ HttpGet httpget; HttpClient httpClient = new DefaultHttpClient(); try { httpget = new HttpGet(Url); } catch (IllegalArgumentException iae) { return null; } HttpResponse response = httpClient.execute(httpget); InputStream contentStream = null; try { StatusLine statusLine = response.getStatusLine(); if (statusLine == null) { throw new IOException(String.format("Unable to get a response from server")); } int statusCode = statusLine.getStatusCode(); if (statusCode < 200 && statusCode >= 300) { throw new IOException( String.format("OWM server responded with status code %d: %s", statusCode, statusLine)); } /* Read the response content */ HttpEntity responseEntity = response.getEntity(); contentStream = responseEntity.getContent(); Reader isReader = new InputStreamReader(contentStream); int contentSize = (int) responseEntity.getContentLength(); if (contentSize < 0) contentSize = 8 * 1024; StringWriter strWriter = new StringWriter(contentSize); char[] buffer = new char[8 * 1024]; int n = 0; while ((n = isReader.read(buffer)) != -1) { strWriter.write(buffer, 0, n); } responseBody = strWriter.toString(); contentStream.close(); } catch (IOException e) { throw e; } catch (RuntimeException re) { httpget.abort(); throw re; } finally { if (contentStream != null) contentStream.close(); } return new JSONObject(responseBody); }
From source file:org.opencastproject.remotetest.server.resource.CaptureAdminResources.java
public static HttpResponse agents(TrustedHttpClient client) { return client.execute(new HttpGet(getServiceUrl() + "agents")); }
From source file:com.hackathon.gavin.string.parser.MyAccountParser.java
public static JSONArray httpGetCall(String urlString, String jsonArrayName) { JSONArray result = null;// w ww .jav a2 s. c o m HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(urlString); try { HttpResponse getResponse = client.execute(request); BufferedReader reader = new BufferedReader( new InputStreamReader(getResponse.getEntity().getContent(), "UTF-8")); StringBuilder builder = new StringBuilder(); for (String line = null; (line = reader.readLine()) != null;) { builder.append(line).append("\n"); } //result = new JSONObject(builder.toString()).getJSONArray(jsonArrayName); JSONObject obj = new JSONObject(builder.toString()); result = obj.getJSONArray(jsonArrayName); } catch (Exception e) { System.out.println(e.toString()); } return result; }