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:edu.tum.cs.ias.knowrob.BarcodeWebLookup.java

public static String lookUpEANsearch(String ean) {

    String res = "";
    if (ean != null && ean.length() > 0) {

        HttpClient httpclient = new DefaultHttpClient();
        httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
        //httpclient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-16");

        try {//from   w  w  w.j a  va2s .  co  m

            HttpGet httpget = new HttpGet("http://www.ean-search.org/perl/ean-search.pl?ean=" + ean + "&os=1");
            httpget.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
            httpget.getParams().setParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "ASCII");

            System.out.println(httpget.getURI());

            // Create a response handler

            ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
                public byte[] handleResponse(HttpResponse response)
                        throws ClientProtocolException, IOException {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        return EntityUtils.toByteArray(entity);
                    } else {
                        return null;
                    }
                }
            };

            byte[] response = httpclient.execute(httpget, handler);

            //   String responseBody = httpclient.execute(httpget, handler);

            //            new HeapByteBuffer(responseBody.getBytes(), 0, responseBody.getBytes().length));
            //            
            //            Charset a  = Charset.forName("UTF-8");
            //            a.newEncoder().encode(responseBody.getBytes());
            //            
            //            System.out.println(responseBody);

            // Parse response document
            res = response.toString();

        } catch (UnsupportedEncodingException uee) {
            uee.printStackTrace();
        } catch (ClientProtocolException cpe) {
            cpe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } finally {
            if (httpclient != null) {
                httpclient.getConnectionManager().shutdown();
            }
        }

    }
    return res;
}

From source file:de.vanita5.twittnuker.util.shortener.TweetShortenerUtils.java

/**
 * Shorten long tweets with hotot.in//from   w  w w  .java  2 s. c  o m
 * @param context
 * @param text
 * @param accounts
 * @return shortened tweet
 */
public static String shortWithHototin(final Context context, final String text, final Account[] accounts) {

    String screen_name = null;
    String avatar_url = null;

    if (accounts != null && accounts.length > 0) {
        screen_name = getAccountScreenName(context, accounts[0].account_id);
        avatar_url = getAccountProfileImage(context, accounts[0].account_id);
        avatar_url = avatar_url != null && !avatar_url.isEmpty() ? avatar_url : DEFAULT_AVATAR_URL;
    }

    try {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(HOTOTIN_URL);
        MultipartEntity requestEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        requestEntity.addPart(HOTOTIN_ENTITY_NAME, new StringBody(screen_name));
        requestEntity.addPart(HOTOTIN_ENTITY_AVATAR, new StringBody(avatar_url));
        requestEntity.addPart(HOTOTIN_ENTITY_TEXT, new StringBody(text, Charset.forName("UTF-8")));
        httpPost.setEntity(requestEntity);

        InputStream responseStream;
        BufferedReader br;

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity responseEntity = response.getEntity();
        responseStream = responseEntity.getContent();
        br = new BufferedReader(new InputStreamReader(responseStream));

        String responseLine = br.readLine();
        String tmpResponse = "";
        while (responseLine != null) {
            tmpResponse += responseLine + System.getProperty("line.separator");
            responseLine = br.readLine();
        }
        br.close();

        JSONObject jsonObject = new JSONObject(tmpResponse);

        String result = jsonObject.getString("text");

        return result;

    } catch (UnsupportedEncodingException e) {
        if (Utils.isDebugBuild())
            Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        if (Utils.isDebugBuild())
            Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        if (Utils.isDebugBuild())
            Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
    } catch (JSONException e) {
        if (Utils.isDebugBuild())
            Log.e(LOG_TAG, e.getMessage());
        e.printStackTrace();
    }
    return null;
}

From source file:org.transdroid.core.rssparser.HttpHelper.java

public static String convertStreamToString(InputStream is) {
    try {/*www .  j  a v  a  2 s .c om*/
        return convertStreamToString(is, null);
    } catch (UnsupportedEncodingException e) {
        // Since this is going to use the default encoding, it is never going to crash on an UnsupportedEncodingException
        e.printStackTrace();
        return null;
    }
}

From source file:jp.co.conit.sss.sp.ex1.util.SSSApiUtil.java

/**
 * ???/*from w  ww  .j a va 2  s.  c o m*/
 * 
 * @param signedData
 * @param signature
 * @return
 */
public static SPResult<List<VerifiedProduct>> orderVerify(Context context, String signedData,
        String signature) {

    StringBuilder sbUrl = new StringBuilder();
    sbUrl.append(DOMAIN);
    sbUrl.append("android/order_verify/");
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("token", Util.getToken(Util.getSignature(context))));
    params.add(new BasicNameValuePair("token_method", "ndk"));
    params.add(new BasicNameValuePair("signed_data", signedData));
    params.add(new BasicNameValuePair("signature", signature));

    UrlEncodedFormEntity urlEncodedFormEntity = null;
    try {
        urlEncodedFormEntity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String str = null;
    try {
        str = HttpUtil.post(sbUrl.toString(), urlEncodedFormEntity);
    } catch (Exception e) {
        return SPResult.getSeverErrInstance(context);
    }
    return new VerifiedProductListParser(context).getResult(str);

}

From source file:org.motrice.bpm.hippo.util.ServletUserNameUtil.java

static String getAttributeShibboleth(String name, final HttpServletRequest request) {
    //Encoding issue. See 
    //https://wiki.shibboleth.net/confluence/display/SHIB2/NativeSPAttributeAccess
    String value = (String) request.getAttribute(name);
    if (value != null) {
        try {/*from   w w w  .ja  v  a2s  . co  m*/
            value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return value;
}

From source file:jp.co.conit.sss.sp.ex1.util.SSSApiUtil.java

/**
 * ?????<br>/*  w w  w . ja va2  s  .com*/
 * ?????receipt?????<br>
 * ?????receipt?{@code null}????
 * 
 * @param context
 * @param productId
 * @param receipt
 * @return
 */
public static SPResult<List<DownloadFile>> getFileList(Context context, String productId, String receipt) {

    StringBuilder sbUrl = new StringBuilder();
    sbUrl.append(DOMAIN);
    sbUrl.append("android/files/");

    List<NameValuePair> params = new ArrayList<NameValuePair>();

    params.add(new BasicNameValuePair("token", Util.getToken(Util.getSignature(context))));
    params.add(new BasicNameValuePair("token_method", "ndk"));
    params.add(new BasicNameValuePair("product_id", productId));
    if (receipt != null) {
        params.add(new BasicNameValuePair("receipt", receipt));
    }
    UrlEncodedFormEntity entity = null;
    try {
        entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    String str = null;
    try {
        str = HttpUtil.post(sbUrl.toString(), entity);
    } catch (Exception e) {
        return SPResult.getSeverErrInstance(context);
    }
    return new DownloadFileListParser(context).getResult(str);
}

From source file:com.mingsoft.weixin.http.HttpClientConnectionManager.java

/**
 * ?/*from  ww w  .j  a v a 2  s  .  c  om*/
 * @param postUrl ?
 * @param param ?
 * @param method 
 * @return null
 */
public static String request(String postUrl, String param, String method) {
    URL url;
    try {
        url = new URL(postUrl);

        HttpURLConnection conn;
        conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(30000); // ??)
        conn.setReadTimeout(30000); // ????)
        conn.setDoOutput(true); // post??http?truefalse
        conn.setDoInput(true); // ?httpUrlConnectiontrue
        conn.setUseCaches(false); // Post ?
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestMethod(method);// "POST"GET
        conn.setRequestProperty("Content-Length", param.length() + "");
        String encode = "utf-8";
        OutputStreamWriter out = null;
        out = new OutputStreamWriter(conn.getOutputStream(), encode);
        out.write(param);
        out.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            return null;
        }
        // ??
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        String line = "";
        StringBuffer strBuf = new StringBuffer();
        while ((line = in.readLine()) != null) {
            strBuf.append(line).append("\n");
        }
        in.close();
        out.close();
        return strBuf.toString();
    } catch (MalformedURLException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:kr.moonlightdriver.app.server.Restor.java

/**
 * Form?  Entity?  URL POST./*ww w. j  a v  a2s.c  o  m*/
 * ?: ???.  ? onComplete  ?  .
 *
 * @param url  REST API? URL
 * @param json   POST ? 
 * @param callback / ?  
 */
public static void Call(String url, String json, Callback callback) {
    try {
        new PostTask(url, callback).execute(new ByteArrayEntity(json.getBytes("UTF8")));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:it.mb.whatshare.CallGooGlInbound.java

private static String getURL(String encodedId, String deviceAssignedID) {
    StringBuilder builder = new StringBuilder("http://");
    Random generator = new Random();
    int sum = 0;/*w w  w.j av a 2s  . c  o m*/
    for (int i = 0; i < 8; i++) {
        char rand = CHARACTERS[generator.nextInt(CHARACTERS.length)];
        builder.append(rand);
        // no idea why they set lowercase for domain names...
        sum += CHAR_MAP.get(Character.toLowerCase(rand));
    }
    builder.append("/");
    builder.append(sum);
    builder.append("?model=");
    try {
        builder.append(URLEncoder
                .encode(String.format("%s %s", Utils.capitalize(Build.MANUFACTURER), Build.MODEL), "UTF-8")
                .replaceAll("\\+", "%20"));
        builder.append("&yourid=");
        builder.append(deviceAssignedID);
        builder.append("&id=");
        builder.append(encodedId);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return builder.toString();
}

From source file:com.fhc25.percepcion.osiris.mapviewer.common.restutils.RestClientStream.java

public static void Execute(final RequestMethod method, final String url, final List<NameValuePair> headers,
        final List<NameValuePair> params, final RestListenerStream listener) throws Exception {
    new Thread() {

        @Override/*from w  ww  .j av  a  2  s .  com*/
        public void run() {
            switch (method) {
            case GET: {
                // add parameters
                String combinedParams = "";
                if (params != null) {
                    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;
            }
            }
        }

    }.start();
}