Example usage for java.lang Long toString

List of usage examples for java.lang Long toString

Introduction

In this page you can find the example usage for java.lang Long toString.

Prototype

public static String toString(long i) 

Source Link

Document

Returns a String object representing the specified long .

Usage

From source file:Main.java

public static String postMultiPart(String urlTo, String post, String filepath, String filefield)
        throws ParseException, IOException {
    HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    InputStream inputStream = null;

    String twoHyphens = "--";
    String boundary = "*****" + Long.toString(System.currentTimeMillis()) + "*****";
    String lineEnd = "\r\n";

    String result = "";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1 * 1024 * 1024;

    String[] q = filepath.split("/");
    int idx = q.length - 1;

    try {// w  w  w  . java 2  s .  c  o m
        File file = new File(filepath);
        FileInputStream fileInputStream = new FileInputStream(file);

        URL url = new URL(urlTo);
        connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        outputStream = new DataOutputStream(connection.getOutputStream());
        outputStream.writeBytes(twoHyphens + boundary + lineEnd);
        outputStream.writeBytes("Content-Disposition: form-data; name=\"" + filefield + "\"; filename=\""
                + q[idx] + "\"" + lineEnd);
        outputStream.writeBytes("Content-Type: image/jpeg" + lineEnd);
        outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
        outputStream.writeBytes(lineEnd);

        bytesAvailable = fileInputStream.available();
        bufferSize = Math.min(bytesAvailable, maxBufferSize);
        buffer = new byte[bufferSize];

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        while (bytesRead > 0) {
            outputStream.write(buffer, 0, bufferSize);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
        }

        outputStream.writeBytes(lineEnd);

        // Upload POST Data
        String[] posts = post.split("&");
        int max = posts.length;
        for (int i = 0; i < max; i++) {
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            String[] kv = posts[i].split("=");
            outputStream.writeBytes("Content-Disposition: form-data; name=\"" + kv[0] + "\"" + lineEnd);
            outputStream.writeBytes("Content-Type: text/plain" + lineEnd);
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(kv[1]);
            outputStream.writeBytes(lineEnd);
        }

        outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

        inputStream = connection.getInputStream();
        result = convertStreamToString(inputStream);

        fileInputStream.close();
        inputStream.close();
        outputStream.flush();
        outputStream.close();

        return result;
    } catch (Exception e) {
        Log.e("MultipartRequest", "Multipart Form Upload Error");
        e.printStackTrace();
        return "error";
    }
}

From source file:Main.java

/**
 * Appends the middle part to the given uri builder and returns it
 * @param uriBuilder//from w  ww  .j a v a2 s . com
 * @param cityId
 * @return
 */
@NonNull
private static Uri.Builder appendMiddleToUriBuilder(@NonNull Uri.Builder uriBuilder, long cityId) {
    uriBuilder.appendPath(PATH_FORECAST).appendPath(PATH_DAILY)
            .appendQueryParameter(QUERY_CITY_ID, Long.toString(cityId))
            .appendQueryParameter(QUERY_COUNT, Integer.toString(QUERY_DAILY_FORECAST_COUNT));

    return uriBuilder;
}

From source file:Main.java

static public Document createDefaultMessage(String messagetype) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentbuilder = null;
    try {/*from   ww  w  . j av a2s.  co  m*/
        documentbuilder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }

    Document doc = documentbuilder.newDocument();
    Element root = doc.createElement("message");
    doc.appendChild(root);
    long timestamp = System.currentTimeMillis();
    root.setAttribute("timestamp", Long.toString(timestamp));
    root.setAttribute("type", messagetype);
    return doc;
}

From source file:Main.java

public static String getTimeElementAsDuration(long time, long curTime, boolean future) {
    if (time < 0) {
        return "N/A";
    }/* www. j av a  2  s  . co m*/

    if (time == 0) {
        return "unset";
    }

    long d;
    if (future) {
        d = time - curTime;
    } else {
        d = curTime - time;
    }
    return Long.toString(d);
}

From source file:Main.java

public static void writeLongAttribute(XmlSerializer out, String name, long value) throws IOException {
    out.attribute(null, name, Long.toString(value));
}

From source file:Main.java

/**
 * Calculate the current timestamp since the Epoch, January 1, 1970 00:00 UTC.
 *
 * @return The current timestamp in seconds
 *///w  ww  . j ava2  s .  c  o  m
public static String getCurrentTimestamp() {
    return Long.toString(System.currentTimeMillis() / 1000);
}

From source file:Main.java

/**
 * Add the account ID parameter.//from  www .  java2 s.co m
 */
public static void setAccountId(Uri.Builder b, long accountId) {
    if (accountId != -1) {
        b.appendQueryParameter(ACCOUNT_ID_PARAM, Long.toString(accountId));
    }
}

From source file:Main.java

/**
 * Add the mailbox ID parameter.//from w  ww .ja v a2 s  . co  m
 */
public static void setMailboxId(Uri.Builder b, long mailboxId) {
    if (mailboxId != -1) {
        b.appendQueryParameter(MAILBOX_ID_PARAM, Long.toString(mailboxId));
    }
}

From source file:Main.java

/**
 * Add the message ID parameter./* w ww.j a  va 2  s. c  om*/
 */
public static void setMessageId(Uri.Builder b, long messageId) {
    if (messageId != -1) {
        b.appendQueryParameter(MESSAGE_ID_PARAM, Long.toString(messageId));
    }
}

From source file:Main.java

public static String getLeftDay(String date) {
    if ("".equals(date) || date == null) {
        return "";
    }/*  w  w  w . j ava2  s .  co  m*/
    Date cur = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", getLocale());
    String curTime = sdf.format(cur);

    Date dt = new Date();
    long day = 0;
    try {
        dt = sdf.parse(curTime);
        long l = Long.parseLong(date) - dt.getTime();
        if (l > 0) {
            day = l / (24 * 60 * 60 * 1000);
        } else {
            day = 0;
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }

    return Long.toString(day);
}