Example usage for java.lang Exception toString

List of usage examples for java.lang Exception toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:Main.java

public static String xmlParser(String filepath) {
    try {/*from  ww w.java2  s .  c om*/
        StringBuilder sb;
        try {
            BufferedReader br = new BufferedReader(new FileReader(new File(filepath)));
            String line;
            sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line.trim());
            }
        } finally {

        }
        return sb.toString();
    } catch (Exception e) {
        System.out.println(e.toString());
        return "";
    }

}

From source file:Main.java

public static void closeCursor(Cursor c) {
    try {/*from  ww  w . j  a v a 2s.  c  o m*/
        if (c != null && c.isClosed() != true) {
            c.close();
            c = null;
        }
    } catch (Exception e) {
        Log.e("xs", "SingleBook onDestroy:" + e.toString());
    }
}

From source file:Main.java

public static String findJniLibrary(Context context, String libName) {
    String result = null;//from  w  w w  .  jav a 2 s  . co m
    ClassLoader classLoader = (context.getClassLoader());
    if (classLoader != null) {
        try {
            Method findLibraryMethod = classLoader.getClass().getMethod("findLibrary",
                    new Class<?>[] { String.class });
            if (findLibraryMethod != null) {
                Object objPath = findLibraryMethod.invoke(classLoader, new Object[] { libName });
                if (objPath != null && objPath instanceof String) {
                    result = (String) objPath;
                }
            }
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
    }

    return result;
}

From source file:Main.java

public static byte[] hexToBytes(String hex) {
    try {//  w  w  w . j  a v a2s  .  co  m
        int length = hex.length();
        byte[] bytes = new byte[length / 2];
        for (int i = 0; i < length; i += 2) {
            bytes[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4)
                    + Character.digit(hex.charAt(i + 1), 16));
        }
        return bytes;
    } catch (Exception e) {
        Log.e(TAG, "Got Exception: " + e.toString());
        return new byte[0];
    }
}

From source file:Main.java

public static String getDaysBetween(String date1, String date2) {
    // input is expected to be exactly like; 2011-01-05
    // date2 must be before date1
    String result = "";

    try {/*from  ww w . j  a v a 2s.  c  om*/

        Date dateOne = DateUtils.parseDate(date1, new String[] { "yyyy-MM-dd" });
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(dateOne);

        Date dateTwo = DateUtils.parseDate(date2, new String[] { "yyyy-MM-dd" });
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(dateTwo);

        long diff = dateOne.getTime() - dateTwo.getTime();
        Log.d(TAG, "days in between:" + (TimeUnit.MILLISECONDS.toSeconds(diff) / 60 / 60 / 24));
    } catch (Exception ex) {
        Log.w(TAG, ex.toString());
    }

    return result;
}

From source file:Main.java

/**
 * Code adapted from http://stackoverflow.com/a/12854981
 * @return Device external IP address or ERROR statement.
 *///from  w w  w. jav  a2s .c  o  m
public static String getExternalIP() {
    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
        // HttpGet httpget = new HttpGet("http://whatismyip.com.au/");
        // HttpGet httpget = new HttpGet("http://www.whatismyip.org/");
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null && entity.getContentLength() > 0) {
            JSONObject json_data = new JSONObject(EntityUtils.toString(entity));
            return json_data.getString("ip");
        } else {
            Log.e(LOG_TAG, "Response error: " + response.getStatusLine().toString());
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, e.toString());
    }
    return "ERROR";
}

From source file:Main.java

/**
 * Converts an XML document to a formatted XML string.
 * /*from   w w w  .j  av a 2  s  .  c  o  m*/
 * @param doc The document to format.
 * @return Formatted XML document.
 */
public static String toString(Document doc) {
    if (doc == null) {
        return "";
    }

    try {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, result);
        return writer.toString();
    } catch (Exception e) {
        return e.toString();
    }
}

From source file:Main.java

public static boolean saveRes(Context gContext, int res, String dirjpg) {
    InputStream is = gContext.getApplicationContext().getResources().openRawResource(res);
    boolean rez = false;
    try {//from   w w w.  j a  va 2s . co m
        FileOutputStream rtFOS = new FileOutputStream(dirjpg + "/" + res + ".gif");
        byte[] buffer = new byte[4096];
        int n = -1;
        while ((n = is.read(buffer, 0, 4095)) != -1) {
            if (n > 0)
                rtFOS.write(buffer, 0, n);
            rez = true;
        }
        rtFOS.flush();
        rtFOS.close();
    } catch (Exception e) {
        Log.e("FullscreenActivity", e.toString());
    }
    return rez;
}

From source file:Main.java

public static Date getDate(final Date aDate) {
    try {/*from w w w. ja  v a2 s  .  c  o m*/
        Calendar calDate = Calendar.getInstance();
        calDate.setTime(aDate);

        return calDate.getTime();

    } catch (Exception ex) {
        System.out.println("HelperTool.getDate(): " + ex.toString());
        return null;
    }
}

From source file:Main.java

public static boolean isOnline(Context ctx) {
    try {/* ww w.jav  a  2  s  . c  o m*/
        ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        return cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnectedOrConnecting();

    } catch (Exception ex) {
        if (enableLogs)
            Log.e(TAG, ex.toString());
    }
    return false;
}