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.devandroid.tkuautowifi.WifiClient.java

public static void login(final Context context, String username, String password,
        final LoginCallback callback) {
    final String url = "http://163.13.8.254/cgi-bin/ace_web_auth.cgi";

    String payload = String.format("username=%s&userpwd=%s", username, password);
    payload += "&login=%E7%99%BB%E5%85%A5";

    ByteArrayEntity entity = null;//from w  w  w.  j a v a 2s  .c  om
    try {
        entity = new ByteArrayEntity(payload.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    mClient.post(context, url, entity, "application/x-www-form-urlencoded", new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, String content) {
            super.onSuccess(statusCode, content);

            if (statusCode == 200) {
                // if login success, the response contents should
                // contain these words.
                if (content.contains("login_online_detail.php")) {
                    if (callback != null) {
                        callback.onSuccess();
                    }
                } else {
                    if (callback != null) {
                        callback.onFailure(context.getString(R.string.login_error));
                    }
                }
            } else {
                if (callback != null) {
                    callback.onFailure(context.getString(R.string.login_timeout));
                }
            }
        }

        @Override
        public void onFailure(Throwable error, String content) {
            super.onFailure(error, content);

            if (callback != null) {
                callback.onFailure(context.getString(R.string.login_timeout));
            }
        }
    });
}

From source file:com.orange.labs.sdk.session.AuthSession.java

/**
 * Static method to encode (UTF-8 + Base64)
 * @param key AppKey of service/*from  w ww . j  a v  a2  s .  c o  m*/
 * @param secret AppSecret of service
 * @return the encoded credentials
 */
public static String encodedCredentials(final String key, final String secret) {

    String credentials = key + ":" + secret;

    byte[] encodedBytes;
    encodedBytes = Base64.encode(credentials.getBytes(), 0);
    String result = "";
    try {
        result = new String(encodedBytes, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.ushahidi.android.app.net.UshahidiHttpClient.java

public static HttpResponse PostURL(String URL, List<NameValuePair> data, String Referer) throws IOException {
    UshahidiPref.httpRunning = true;//from   ww w  .j a  va2 s  .co  m
    // Dipo Fix
    try {
        // wrap try around because this constructor can throw Error
        final HttpPost httpost = new HttpPost(URL);
        // org.apache.http.client.methods.
        if (Referer.length() > 0) {
            httpost.addHeader("Referer", Referer);
        }
        if (data != null) {
            try {
                // NEED THIS NOW TO FIX ERROR 417
                httpost.getParams().setBooleanParameter("http.protocol.expect-continue", false);
                httpost.setEntity(new UrlEncodedFormEntity(data, HTTP.UTF_8));
            } catch (final UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                UshahidiPref.httpRunning = false;
                return null;
            }
        }

        // Post, check and show the result (not really spectacular, but
        // works):
        try {
            HttpResponse response = UshahidiService.httpclient.execute(httpost);
            UshahidiPref.httpRunning = false;
            return response;

        } catch (final Exception e) {

        }
    } catch (final Exception e) {
        e.printStackTrace();
    }

    UshahidiPref.httpRunning = false;
    return null;
}

From source file:org.wso2.cdm.agent.proxy.ServerApiAccess.java

public static Map<String, String> postData(APIUtilities apiUtilities, Map<String, String> headers) {
    String httpMethod = apiUtilities.getHttpMethod();
    String url = apiUtilities.getEndPoint();
    JSONObject params = apiUtilities.getRequestParams();
    Map<String, String> responseParams = new HashMap<String, String>();
    HttpClient httpclient = getCertifiedHttpClient();

    if (httpMethod.equals("POST")) {
        HttpPost httpPost = new HttpPost(url);
        if (params != null) {
            try {
                httpPost.setEntity(new StringEntity(params.toString()));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }//from   w  w  w.  j  a  va 2s .c  om
        } else {
            httpPost.setEntity(null);
        }
        Log.e("url", "" + url);
        HttpPost httpPostWithHeaders = (HttpPost) buildHeaders(httpPost, headers, httpMethod);
        try {
            HttpResponse response = httpclient.execute(httpPostWithHeaders);
            String status = String.valueOf(response.getStatusLine().getStatusCode());
            Log.d(TAG, status);
            responseParams.put("response", getResponseBody(response));
            responseParams.put("status", status);
            return responseParams;
        } catch (ClientProtocolException e) {
            Log.d(TAG, "ClientProtocolException :" + e.toString());
            return null;
        } catch (IOException e) {
            Log.d(TAG, e.toString());
            responseParams.put("response", "Internal Server Error");
            responseParams.put("status", "500");
            return responseParams;
        }
    } else if (httpMethod.equals("GET")) {
        //          if(payload!=null){
        //             url = url+"?"+payload;
        //          }
        HttpGet httpGet = new HttpGet(url);
        HttpGet httpGetWithHeaders = (HttpGet) buildHeaders(httpGet, headers, httpMethod);
        Log.d(TAG, httpGetWithHeaders.toString() + " GET");
        try {
            HttpResponse response = httpclient.execute(httpGetWithHeaders);
            responseParams.put("response", getResponseBody(response));
            responseParams.put("status", String.valueOf(response.getStatusLine().getStatusCode()));
            return responseParams;
        } catch (ClientProtocolException e) {
            Log.d(TAG, "ClientProtocolException :" + e.toString());
            return null;
        } catch (IOException e) {
            Log.d(TAG, e.toString());
            responseParams.put("response", "Internal Server Error");
            responseParams.put("status", "500");
            return responseParams;
        }
    }
    return null;
}

From source file:com.mingsoft.util.proxy.Proxy.java

/**
 * post//w w  w  .  jav a  2s  . com
 * 
 * @param url
 *            ?<br>
 * @param header
 *            ?Header new Header()<br>
 * @param params
 *            ?<br>
 * @param encoding
 *            ??<br>
 * @return Result 
 */
public static Result post(String url, com.mingsoft.util.proxy.Header header, Map params, String encoding) {
    //log.info("?" + url);
    // Httpclient
    DefaultHttpClient client = new DefaultHttpClient();
    // post
    HttpPost httpPost = new HttpPost(url);
    // cookie?()
    httpPost.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
    // ????
    if (params != null) {
        List list = new ArrayList();

        Iterator it = params.keySet().iterator();
        while (it.hasNext()) {
            String temp = (String) it.next();
            list.add(new BasicNameValuePair(temp, params.get(temp) + ""));
        }

        try {
            httpPost.setEntity(new UrlEncodedFormEntity(list, encoding));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }
    // 
    if (null != header && header.getHeaders().size() > 0) {
        httpPost.setHeaders(Proxy.assemblyHeader(header.getHeaders()));
    }
    // 
    HttpResponse response = null;
    try {
        response = client.execute(httpPost);
        HttpEntity entity = response.getEntity();
        // 
        // httpPost.abort();
        // ??
        Result result = new Result();
        // ??
        result.setStatusCode(response.getStatusLine().getStatusCode());
        // ?
        result.setHeaders(response.getAllHeaders());
        // cookie
        result.setCookie(assemblyCookie(client.getCookieStore().getCookies()));
        // ?
        result.setHttpEntity(entity);
        return result;
    } catch (ClientProtocolException e) {
        e.printStackTrace();
        log.error(e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        log.error(e.getMessage());
    }
    return null;
}

From source file:co.cask.cdap.gateway.util.Util.java

/**
 * URL-encode a binary string./*from  w  ww  .  j ava 2s. co  m*/
 */
public static String urlEncode(byte[] binary) {
    if (binary == null) {
        return null;
    }
    try { // we use a base encoding that accepts all byte values
        return URLEncoder.encode(new String(binary, "ISO8859_1"), "ISO8859_1");
    } catch (UnsupportedEncodingException e) {
        // this cannot happen with ISO8859_1 = Latin1
        e.printStackTrace();
        return null;

    }
}

From source file:net.sourceforge.doddle_owl.utils.Utils.java

/**
 * Resource?getLocalName??????????????????????????
 * ?????/*w ww . jav  a  2  s. co m*/
 */
public static String getLocalName(Resource res) {
    String ns = getNameSpace(res);
    String localName = res.getURI().replaceAll(ns, "");
    if (ns.equals(DODDLEConstants.JWO_URI)) {
        try {
            localName = URLDecoder.decode(localName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    return localName;
}

From source file:jeeves.server.sources.ServiceRequestFactory.java

private static String simplifyName(String file) {
    //--- get the file name without path

    file = new File(file).getName();

    //--- the previous getName method is not enough
    //--- with IE and a server running on Linux we still have a path problem

    int pos1 = file.lastIndexOf("\\");
    int pos2 = file.lastIndexOf('/');

    int pos = Math.max(pos1, pos2);

    if (pos != -1)
        file = file.substring(pos + 1).trim();

    //--- we need to sanitize the filename here - make it UTF8, no ctrl
    //--- characters and only containing [A-Z][a-z][0-9],_.-

    //--- start by converting to UTF-8
    try {//from ww  w . j av  a  2  s.c  om
        byte[] utf8Bytes = file.getBytes("UTF8");
        file = new String(utf8Bytes, "UTF8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    //--- replace whitespace with underscore
    file = file.replaceAll("\\s", "_");

    //--- remove everything that isn't [0-9][a-z][A-Z],#_.-
    file = file.replaceAll("[^\\w&&[^,_.-]]", "");
    return file;
}

From source file:es.ucm.look.data.remote.restful.RestMethod.java

/**
 * Used to insert an element//from w w w  .  j  a  v a  2 s  .co m
 * 
 * @param url
 *       Element URI
 * @param c
 *       The element represented with a JSON
 * @return
 *       The response
 */
public static HttpResponse doPost(String url, JSONObject c) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);
    StringEntity s = null;
    try {
        s = new StringEntity(c.toString());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);
    request.addHeader("accept", "application/json");

    try {
        return httpclient.execute(request);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:es.ucm.look.data.remote.restful.RestMethod.java

/**
 * To Update an element//from  w  w  w .  j a  va 2 s . com
 * 
 * @param url
 *       Element URI
 * @param c
 *       The element to update represented with a JSON
 * @return
 *       The response
 */
public static HttpResponse doPut(String url, JSONObject c) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPut request = new HttpPut(url);
    StringEntity s = null;
    try {
        s = new StringEntity(c.toString());
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    s.setContentEncoding("UTF-8");
    s.setContentType("application/json");

    request.setEntity(s);
    request.addHeader("accept", "application/json");

    try {
        return httpclient.execute(request);
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}