コード例 #1
0
  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;
  }
コード例 #2
0
    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();
    }
コード例 #3
0
  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();
  }
コード例 #4
0
  /**
   * Takes a string and adds to it, with a separator, if the bit to be added isn't null. Since the
   * logger takes so many arguments that might be null, this method helps cut out some of the
   * agonizing tedium of writing the same 3 lines over and over.
   *
   * @param source StringBuilder containing the text to append to.
   * @param addStr The String to append
   * @param delimiter The String to separate the source and appended strings. A tab or comma, for
   *     instance.
   * @return The fully concatenated String as a StringBuilder
   */
  private StringBuilder appendIfNotNull(StringBuilder source, String addStr, String delimiter) {
    if (addStr != null) {
      if (addStr.length() == 0) {
        delimiter = "";
      }

      return source.append(addStr).append(delimiter);
    }
    return source;
  }