Example usage for java.io UnsupportedEncodingException printStackTrace

List of usage examples for java.io UnsupportedEncodingException printStackTrace

Introduction

In this page you can find the example usage for java.io UnsupportedEncodingException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static String getJSONFromUrl(String url) {

    try {//from   w w w  .ja v a  2 s.co  m
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        //            httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

/**
 * Convert UTF-8 byte array to String.//from  w ww  . ja v  a 2 s .co m
 * @param buf
 * @return
 */
/* package protected */static String convertUTF8ToUTF16(byte[] buf) {
    try {
        return new String(buf, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        ex.printStackTrace();
        return bytesToString(buf, 0, buf.length);
    }
}

From source file:Main.java

public static String getJSONFromUrlWithPostRequest(String url, List<NameValuePair> params) {

    try {//ww  w.  ja  v a  2 s.c  om
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        String tempResponse = EntityUtils.toString(httpResponse.getEntity());

        return tempResponse;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return "";
}

From source file:Main.java

/**
 * @param text//from   w ww  . ja  va  2  s .  c  om
 * @return
 */
public static String convert2UTF8(String text) {

    try {
        byte[] utf8Bytes = text.getBytes("UTF-8");
        text = new String(utf8Bytes, "UTF-8");

    } catch (java.io.UnsupportedEncodingException e) {
        e.printStackTrace();

    }

    return text;

}

From source file:Main.java

private static Element getRootNodeFromXmlStr(String xmlStr) {
    Element element = null;/*ww w .ja va 2  s .c o m*/
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputStream is = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
        Document document = db.parse(is);
        element = document.getDocumentElement();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return element;
}

From source file:Main.java

public static File writeStringToTextFile(String outputFile, String string) {

    try {//from   w w w .j a  v a 2s. c om
        return getFileFromBytes(string.getBytes("UTF-8"), outputFile);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

public static Bundle decodeUrl(String s) {
    Bundle params = new Bundle();
    if (s != null) {
        String array[] = s.split("&");
        for (String parameter : array) {
            String v[] = parameter.split("=");
            try {
                params.putString(URLDecoder.decode(v[0], "UTF-8"), URLDecoder.decode(v[1], "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();

            }/*from www. j  a  v a2 s .  c o m*/
        }
    }
    return params;
}

From source file:Main.java

public static String truncate(String str, int byteLength) {
    if (str == null) {
        return null;
    }/*ww  w .j  av  a  2s .c om*/
    if (str.length() == 0) {
        return str;
    }
    if (byteLength < 0) {
        throw new IllegalArgumentException("Parameter byteLength must be great than 0");
    }
    int i = 0;
    int len = 0;
    int leng = 0;
    char[] chs = str.toCharArray();
    try {
        leng = str.getBytes("gbk").length;

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    if (leng <= byteLength)
        return str;
    while ((len < byteLength) && (i < leng)) {
        len = (chs[i++] > 0xff) ? (len + 2) : (len + 1);
    }

    if (len > byteLength) {
        i--;
    }
    return new String(chs, 0, i) + "...";
}

From source file:Main.java

public static String parseUnicodeString(String source) {
    try {//w w w  .  j a v  a  2 s.  co  m
        source = URLDecoder.decode(source, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }
    return source;
}

From source file:Main.java

public static String getSign(String appid, String q, String salt, String key) {
    try {/*from   ww w  .  j  a va  2 s . c  om*/
        q = new String(q.getBytes(), "UTF-8");
        return stringToMD5(new StringBuilder(appid).append(q).append(salt).append(key).toString());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
}