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:com.fhc25.percepcion.osiris.mapviewer.common.restutils.RestClient.java

/**
 * Executes the requests./*from ww  w  .  j av  a 2  s  . c om*/
 *
 * @param method   the type of method (POST, GET, DELETE, PUT).
 * @param url      the url of the request.
 * @param headers  the headers to include.
 * @param params   the params to include (if any)
 * @param listener a listener for callbacks.
 * @throws Exception
 */
public static void Execute(final RequestMethod method, final String url, final List<NameValuePair> headers,
        final List<NameValuePair> params, final RestListener listener) throws Exception {
    new Thread() {
        @Override
        public void run() {
            switch (method) {
            case GET: {
                // add parameters
                String combinedParams = "";
                if (params != null) {
                    if (!params.isEmpty()) {
                        combinedParams += "?";
                    }
                    for (NameValuePair p : params) {

                        String paramString = "";
                        try {
                            paramString = p.getName() + "="
                                    + URLEncoder.encode(p.getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        if (combinedParams.length() > 1)
                            combinedParams += "&" + paramString;
                        else
                            combinedParams += paramString;
                    }
                }
                HttpGet request = new HttpGet(url + combinedParams);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }
                executeRequest(request, url, listener);
                break;
            }

            case POST: {
                HttpPost request = new HttpPost(url);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }
                if (params != null) {
                    try {
                        if (params.get(0).getName() == "json") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.UTF_8);
                            //se.setContentEncoding( HTTP.DEFAULT_CONTENT_CHARSET);

                            request.setEntity(se);
                            Lgr.v(TAG, "Sending json parameter: " + params.get(0).getValue());
                            if (se != null) {
                                Lgr.v(TAG, "Entity string: " + se.toString());
                                if (se.getContentEncoding() != null) {
                                    Lgr.v(TAG, "Entity encoding: "
                                            + request.getEntity().getContentEncoding().getValue());
                                }
                            }

                        } else if (params.get(0).getName() == "xml") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending xml parameter: " + params.get(0).getValue());
                        } else {
                            request.setEntity(new UrlEncodedFormEntity(params, HTTP.DEFAULT_CONTENT_CHARSET));
                            Lgr.v(TAG, "Sending UrlEncodedForm parameters ");
                        }

                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                Lgr.v(TAG, "Request " + request.toString());
                executeRequest(request, url, listener);

                break;
            }

            case PUT: {
                HttpPut request = new HttpPut(url);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }
                if (params != null) {
                    try {
                        if (params.get(0).getName() == "json") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending json parameter: " + params.get(0).getValue());
                        } else if (params.get(0).getName() == "xml") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending xml parameter: " + params.get(0).getValue());
                        } else if (params.get(0).getName() == "string") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending string parameter: " + params.get(0).getValue());
                        } else {
                            request.setEntity(new UrlEncodedFormEntity(params, HTTP.DEFAULT_CONTENT_CHARSET));
                            Lgr.v(TAG, "Sending UrlEncodedForm parameters ");
                        }

                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                Lgr.v(TAG, "Request " + request.toString());
                executeRequest(request, url, listener);

                break;
            }

            case PATCH: {
                HttpPatch request = new HttpPatch(url);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }
                if (params != null) {
                    try {
                        if (params.get(0).getName() == "json") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending json parameter: " + params.get(0).getValue());
                        } else if (params.get(0).getName() == "xml") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending xml parameter: " + params.get(0).getValue());
                        } else if (params.get(0).getName() == "string") {
                            se = new StringEntity(params.get(0).getValue(), HTTP.DEFAULT_CONTENT_CHARSET);
                            request.setEntity(se);
                            Lgr.v(TAG, "Sending string parameter: " + params.get(0).getValue());
                        } else {
                            request.setEntity(new UrlEncodedFormEntity(params, HTTP.DEFAULT_CONTENT_CHARSET));
                            Lgr.v(TAG, "Sending UrlEncodedForm parameters ");
                        }

                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                Lgr.v(TAG, "Request " + request.toString());
                executeRequest(request, url, listener);

                break;
            }

            case HEAD: {
                HttpHead request = new HttpHead(url);
                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }

                Lgr.v(TAG, "Request " + request.toString());

                executeRequest(request, url, listener);

                break;
            }

            case DELETE: {
                HttpDelete request = new HttpDelete(url);

                // add headers
                if (headers != null) {
                    for (NameValuePair h : headers)
                        request.addHeader(h.getName(), h.getValue());
                }

                executeRequest(request, url, listener);

                break;
            }

            }
        }
    }.start();
}

From source file:jade.mtp.http.XMLCodec.java

/** 
 * A user-defined property (String name, Object value) is encoded the following way:
 * <user-defined href="name" type="type">value</user-defined>
 * //from  w w  w.j a  va  2 s  . c o  m
 */
private static void encodeProp(StringBuffer sb, Property p) {
    String v = null;
    Object o = p.getValue();
    String type = PROP_STRING_TYPE;
    if (o instanceof String) {
        v = (String) o;
    } else if (o instanceof byte[]) {
        type = PROP_BYTE_TYPE;
        try {
            v = new String(Base64.encodeBase64((byte[]) o), "US-ASCII");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } else if (o instanceof Serializable) {
        type = PROP_SER_TYPE;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(o);
            oos.close();
            byte[] bytes = bos.toByteArray();
            if (bytes != null)
                v = new String(Base64.encodeBase64(bytes), "US-ASCII");
        } catch (IOException ioe) {
            return;
        }
    } else {
        return;
    }
    sb.append(OT).append(PROP_TAG).append(" ");
    sb.append(PROP_ATTR).append("=\"").append(p.getName()).append("\" ");
    sb.append(PROP_ATTR_TYPE).append("=\"").append(type).append("\"");
    sb.append(CT);
    sb.append(v);
    sb.append(ET).append(PROP_TAG).append(CT);
}

From source file:org.lukang.weibo.net.Utility.java

public static String encodeUrl(WeiboParameters parameters) {
    if (parameters == null) {
        return "";
    }/* ww  w. j av a  2 s . c o  m*/

    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (int loc = 0; loc < parameters.size(); loc++) {
        if (first)
            first = false;
        else
            sb.append("&");
        try {
            sb.append(URLEncoder.encode(parameters.getKey(loc), "utf-8") + "="
                    + URLEncoder.encode(parameters.getValue(loc), "utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:org.lukang.weibo.net.Utility.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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }// w w  w .  jav  a  2  s. co  m
        }
    }
    return params;
}

From source file:com.dandelion.memberapp.util.Base64.java

public static String decodeToString(String str, int flags, String charset) {
    try {/*w w w.j a  v a2s. c om*/
        return new String(decode(str, flags), charset);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:com.dandelion.memberapp.util.Base64.java

public static String encodeToString(String input, int flags, String charset) {
    try {/*ww w .ja v  a 2 s .  c  om*/
        return encodeToString(input.getBytes(charset), flags);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}

From source file:org.web4thejob.web.util.ZkUtil.java

public static void downloadCsv(File file) {
    if (file != null) {
        try {//from  w  w  w.  j  av a 2  s  . c om
            Filedownload.save(new InputStreamReader(new FileInputStream(file),
                    CoreUtil.getParameterValue(Category.PRINTER_PARAM, Key.CHARSET, String.class, "UTF-8")) {
            }, "text/csv", "export.csv");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException("printing failed");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("printing failed");
        }
    }
}

From source file:com.almarsoft.GroundhogReader.lib.MessageTextProcessor.java

public static String decodeFrom(Field fromField, String charset, Message message) {

    String rawStr = new String(fromField.getRaw().toByteArray());
    if (rawStr.indexOf("=?") != -1) {
        MailboxList authorList = message.getFrom();
        Mailbox author;/*from   w  w  w. j av a 2s  . com*/

        if ((authorList != null) && (author = authorList.get(0)) != null)
            return author.getName() + "<" + author.getAddress() + ">";
        else {
            // Parse error on mime4j; some people use emails like "bla@@@bla.com", some stuuuuuupid servers accept them, and mime4j
            // gives (rightly) a parse error
            return DecoderUtil.decodeEncodedWords(fromField.getBody().trim());
        }
    }

    try {
        return new String(fromField.getRaw().toByteArray(), charset).replaceFirst("From: ", "");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return "Unknown";
    }
}

From source file:com.haoqee.chat.net.Utility.java

public static String encodeUrl(Parameters parameters) {
    if (parameters == null) {
        return "";
    }/*from   w w w  .  jav  a2s.  com*/

    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (int loc = 0; loc < parameters.size(); loc++) {
        if (first)
            first = false;
        else {
            sb.append("&");
        }
        try {
            sb.append(URLEncoder.encode(parameters.getKey(loc), "UTF-8") + "="
                    + URLEncoder.encode(parameters.getValue(loc), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return sb.toString();
}

From source file:com.research.net.Utility.java

public static String encodeUrl(ResearchParameters parameters) {
    if (parameters == null) {
        return "";
    }/*w w  w.ja  v  a 2 s .c o m*/

    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (int loc = 0; loc < parameters.size(); loc++) {
        if (first)
            first = false;
        else {
            sb.append("&");
        }
        try {
            sb.append(URLEncoder.encode(parameters.getKey(loc), "UTF-8") + "="
                    + URLEncoder.encode(parameters.getValue(loc), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return sb.toString();
}