List of usage examples for java.io BufferedReader read
public int read() throws IOException
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doPOST(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;//from w ww. j a v a 2 s . c o m URL url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // connection.setRequestProperty("token",token); connection.setConnectTimeout(30000); connection.setReadTimeout(30000); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); String content = ""; if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } out.writeBytes(content.substring(1)); } out.flush(); out.close(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:com.breadwallet.tools.threads.ImportPrivKeyTask.java
private static String callURL(String myURL) { // System.out.println("Requested URL_EA:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null;/*from w w w . j a va 2s . co m*/ try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) urlConn.setReadTimeout(60 * 1000); if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } assert in != null; in.close(); } catch (Exception e) { return null; } return sb.toString(); }
From source file:com.breadwallet.tools.util.JsonParser.java
private static String callURL(String myURL) { // System.out.println("Requested URL_EA:" + myURL); StringBuilder sb = new StringBuilder(); HttpURLConnection urlConn = null; InputStreamReader in = null;//w ww .j a v a 2 s .com try { URL url = new URL(myURL); urlConn = (HttpURLConnection) url.openConnection(); int versionNumber = 0; MainActivity app = MainActivity.app; if (app != null) { try { PackageInfo pInfo = null; pInfo = app.getPackageManager().getPackageInfo(app.getPackageName(), 0); versionNumber = pInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } } int stringId = 0; String appName = ""; if (app != null) { stringId = app.getApplicationInfo().labelRes; appName = app.getString(stringId); } String message = String.format(Locale.getDefault(), "%s/%d/%s", appName.isEmpty() ? "breadwallet" : appName, versionNumber, System.getProperty("http.agent")); urlConn.setRequestProperty("User-agent", message); urlConn.setReadTimeout(60 * 1000); String strDate = urlConn.getHeaderField("date"); if (strDate == null || app == null) { Log.e(TAG, "callURL: strDate == null!!!"); } else { @SuppressWarnings("deprecation") long date = Date.parse(strDate) / 1000; SharedPreferencesManager.putSecureTime(app, date); Assert.assertTrue(date != 0); } if (urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } assert in != null; in.close(); } catch (Exception e) { return null; } return sb.toString(); }
From source file:Main.java
/** * Get AppName(package name) Name by specific process id. * * @return App Name, ex) com.api.demo//from www . jav a2 s .c o m */ public static String getMyAppName(int pid) { final String PATH = "/proc/" + pid + "/cmdline"; BufferedReader cmdlineReader = null; StringBuilder processName = new StringBuilder(); int c = 0; try { cmdlineReader = new BufferedReader(new InputStreamReader(new FileInputStream(PATH))); while ((c = cmdlineReader.read()) > 0) { processName.append((char) c); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (cmdlineReader != null) cmdlineReader.close(); } catch (IOException e) { e.printStackTrace(); } } return processName.toString(); }
From source file:org.apache.hadoop.dfs.DFSTestUtil.java
static String readFile(File f) throws IOException { StringBuilder b = new StringBuilder(); BufferedReader in = new BufferedReader(new FileReader(f)); for (int c; (c = in.read()) != -1; b.append((char) c)) ;/*from ww w . j a va2s. co m*/ in.close(); return b.toString(); }
From source file:dataflow.feed.api.Weather.java
/** * Method which builds the string of the URL to call * @param myURL the URL which must become a callURL * @return /*from w ww . ja v a 2s. c o m*/ */ public static String callURL(String myURL) { //System.out.println("Requested URL:" + myURL); StringBuilder sb = new StringBuilder(); URLConnection urlConn = null; InputStreamReader in = null; try { URL url = new URL(myURL); urlConn = url.openConnection(); if (urlConn != null) urlConn.setReadTimeout(60 * 1000); if (urlConn != null && urlConn.getInputStream() != null) { in = new InputStreamReader(urlConn.getInputStream(), Charset.defaultCharset()); BufferedReader bufferedReader = new BufferedReader(in); if (bufferedReader != null) { int cp; while ((cp = bufferedReader.read()) != -1) { sb.append((char) cp); } bufferedReader.close(); } } in.close(); } catch (Exception e) { throw new RuntimeException("Exception while calling URL:" + myURL, e); } return sb.toString(); }
From source file:eu.project.ttc.test.unit.TestUtil.java
public static String readFile(Reader reader) { try {/* w ww . j a v a 2 s . co m*/ BufferedReader br = new BufferedReader(reader); try { StringBuilder sb = new StringBuilder(); int c; while ((c = br.read()) != -1) { sb.append((char) c); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); return null; } finally { br.close(); } } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:crawlers.Xinh.java
private static JsonObject readJsonObjectFromUrl(String url) throws IOException { InputStream is = new URL(url).openStream(); JsonObject jsonObj = null;//ww w .j a v a 2 s. co m try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } String jsonText = sb.toString(); if (jsonText != null && jsonText.length() > 0) { JsonParser jsonParser = new JsonParser(); jsonObj = jsonParser.parse(jsonText).getAsJsonObject(); if (jsonObj != null) { return jsonObj; } } return jsonObj; } finally { is.close(); } }
From source file:crawlers.Xinh.java
private static JsonArray readJsonFromUrl(String url) throws IOException { InputStream is = new URL(url).openStream(); JsonArray json = null;//from ww w . j a va2 s . c o m try { BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } String jsonText = sb.toString(); if (jsonText != null && jsonText.length() > 0) { JsonParser jsonParser = new JsonParser(); JsonObject jsonObj = jsonParser.parse(jsonText).getAsJsonObject(); if (jsonObj != null) { JsonElement jsonElement = jsonObj.get(url); if (jsonElement != null) { JsonElement jsonElementSub = jsonElement.getAsJsonObject().get("comments"); if (jsonElementSub != null) json = jsonElementSub.getAsJsonObject().get("data").getAsJsonArray(); } } } return json; } finally { is.close(); } }
From source file:crawlers.Xinh.java
private static HashMap<String, JsonArray> readMapJsonFromUrl(List<String> listUrl) throws IOException { String listURLString = StringUtils.join(listUrl, ","); InputStream is = new URL("https://graph.facebook.com/comments/?ids=" + listURLString).openStream(); HashMap<String, JsonArray> returnValue = new HashMap<String, JsonArray>(); try {/* w ww . j a va 2 s .c om*/ BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); StringBuilder sb = new StringBuilder(); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } String jsonText = sb.toString(); if (jsonText != null && jsonText.length() > 0) { JsonParser jsonParser = new JsonParser(); JsonObject jsonObj = jsonParser.parse(jsonText).getAsJsonObject(); if (jsonObj != null) { for (String tmpUrl : listUrl) { JsonElement jsonElement = jsonObj.get(tmpUrl); JsonArray json = null; if (jsonElement != null) { JsonElement jsonElementSub = jsonElement.getAsJsonObject().get("comments"); if (jsonElementSub != null) json = jsonElementSub.getAsJsonObject().get("data").getAsJsonArray(); } if (json != null) returnValue.put(tmpUrl, json); } } } return returnValue; } finally { is.close(); } }