public String readFromFile() { String ret = ""; try { InputStream inputStream = context.openFileInput("locations.json"); if (inputStream != null) { InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String receiveString = ""; StringBuilder stringBuilder = new StringBuilder(); while ((receiveString = bufferedReader.readLine()) != null) { stringBuilder.append(receiveString); } inputStream.close(); ret = stringBuilder.toString(); } } catch (FileNotFoundException ex) { Log.e("login activity", "File not found: " + ex.toString()); } catch (IOException ex) { Log.e("login activity", "Can not read file: " + ex.toString()); } return ret; }
private String transportHttpMessage(String message) { URL serverurl; HttpURLConnection connection; try { serverurl = new URL(this.ServerUrl); connection = (HttpURLConnection) serverurl.openConnection(); connection.setDefaultUseCaches(false); connection.setDoInput(true); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); } catch (Exception e) { e.printStackTrace(); return null; } OutputStreamWriter outstream; try { outstream = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); outstream.write(message); outstream.flush(); /* PrintWriter writer = new PrintWriter(outstream); writer.write(message); writer.flush(); */ outstream.close(); } catch (Exception e) { e.printStackTrace(); return null; } StringBuilder strbuilder = new StringBuilder(); try { InputStreamReader instream = new InputStreamReader(connection.getInputStream(), "UTF-8"); BufferedReader reader = new BufferedReader(instream); String str; while ((str = reader.readLine()) != null) strbuilder.append(str + "\r\n"); instream.close(); } catch (Exception e) { e.printStackTrace(); return null; } connection.disconnect(); return strbuilder.toString(); }
public static String slurp(Reader r) { StringBuilder rval = new StringBuilder(); try { char[] buffer = new char[4 << 10]; while (true) { int n = r.read(buffer, 0, buffer.length); if (n < 1) break; rval.append(buffer, 0, n); } } catch (IOException e) { Log.e(LOG_TAG, "malfunction slurping stream", e); } return rval.toString(); }