Example usage for java.net MalformedURLException printStackTrace

List of usage examples for java.net MalformedURLException printStackTrace

Introduction

In this page you can find the example usage for java.net MalformedURLException 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 readPeopleData(String uri) {
    URL url;/*from w ww  .  j av a2 s .  c  om*/
    StringBuffer jsonstring = null;
    HttpURLConnection connection;

    Logger.getAnonymousLogger().info("Getting data for " + uri);

    try {
        url = new URL(uri);
        connection = (HttpURLConnection) url.openConnection();
        connection.setConnectTimeout(1000 * 5);

        InputStreamReader is = new InputStreamReader(connection.getInputStream());
        BufferedReader buff = new BufferedReader(is);
        jsonstring = new StringBuffer();
        String line = "";
        do {
            line = buff.readLine();
            if (line != null)
                jsonstring.append(line);
        } while (line != null);
    }

    catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Logger.getAnonymousLogger().info("Returning Data " + jsonstring);

    if (null != jsonstring) {
        return jsonstring.toString().trim();
    } else {
        return null;
    }
}

From source file:biz.wolschon.finance.jgnucash.helper.HttpFetcher.java

/**
 * Fetch the given URL with http-basic-auth.
 * @param url the URL//  w  w  w .  j a  va 2s  .c o  m
 * @param username username
 * @param password password
 * @return the page-content or null
 */
public static InputStream fetchURLStream(final URL url, final String username, final String password) {

    try {
        String userPassword = username + ":" + password;

        // Encode String
        String encoding = new sun.misc.BASE64Encoder().encode(userPassword.getBytes());

        URLConnection uc = url.openConnection();
        uc.setRequestProperty("Authorization", "Basic " + encoding);
        return uc.getInputStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        LOGGER.error("cannot fetch malformed URL " + url, e);
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        LOGGER.error("cannot fetch URL " + url, e);
        return null;
    }
}

From source file:com.kubotaku.android.openweathermap.lib.util.TimeZoneUtil.java

public static TimeZone getTargetTimeZone(LatLng latlng, String apiKey) {
    TimeZone timeZone = null;//w  ww  .j a  va 2s .  co  m

    try {
        Date now = new Date();
        long currentTime = now.getTime();
        String requestURL = String.format(API_URL, latlng.latitude, latlng.longitude, currentTime, apiKey);

        URL url = new URL(requestURL);
        InputStream is = url.openConnection().getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while (null != (line = reader.readLine())) {
            sb.append(line);
        }
        String data = sb.toString();

        timeZone = TimeZoneParser.parseTimeZone(data);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return timeZone;
}

From source file:Main.java

static InputStream openRemoteInputStream(Uri uri) {
    URL finalUrl;//ww  w. ja v a  2 s  .  c o  m
    try {
        finalUrl = new URL(uri.toString());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    }
    HttpURLConnection connection;
    try {
        connection = (HttpURLConnection) finalUrl.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    connection.setInstanceFollowRedirects(false);
    int code;
    try {
        code = connection.getResponseCode();
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    if ((code == 301) || (code == 302) || (code == 303)) {
        String newLocation = connection.getHeaderField("Location");
        return openRemoteInputStream(Uri.parse(newLocation));
    }
    try {
        return (InputStream) finalUrl.getContent();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:biz.wolschon.finance.jgnucash.helper.HttpFetcher.java

/**
 * Fetch the given URL with http-basic-auth.
 * @param url the URL/* www .j  a  va2  s . c o  m*/
 * @param username username
 * @param password password
 * @return the page-content or null
 */
public static String fetchURL(final URL url, final String username, final String password) {
    StringWriter sw = new StringWriter();

    try {
        PrintWriter pw = new PrintWriter(sw);
        InputStream content = fetchURLStream(url, username, password);
        BufferedReader in = new BufferedReader(new InputStreamReader(content));
        String line;
        while ((line = in.readLine()) != null) {
            pw.println(line);
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
        LOGGER.error("cannot fetch malformed URL " + url, e);
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        LOGGER.error("cannot fetch URL " + url, e);
        return null;
    }
    return sw.toString();
}

From source file:com.ant.myteam.gcm.POST2GCM.java

public static void post(String apiKey, Content content) {

    try {/*w w w  .jav a  2  s.c  om*/

        // 1. URL
        URL url = new URL("https://android.googleapis.com/gcm/send");

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);

        conn.setDoOutput(true);

        // 5. Add JSON data into POST request body

        //`5.1 Use Jackson object mapper to convert Contnet object into JSON
        ObjectMapper mapper = new ObjectMapper();

        // 5.2 Get connection output stream
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        // 5.3 Copy Content "JSON" into
        mapper.writeValue(wr, content);

        // 5.4 Send the request
        wr.flush();

        // 5.5 close
        wr.close();

        // 6. Get the response
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 7. Print result
        System.out.println(response.toString());

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:pushandroid.POST2GCM.java

public static void post(Content content) {

    try {/*w  ww  . j a v a  2  s  .c o m*/

        // 1. URL
        URL url = new URL(URL);

        // 2. Open connection
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        // 3. Specify POST method
        conn.setRequestMethod("POST");

        // 4. Set the headers
        conn.setRequestProperty("Content-Type", "application/json");
        conn.setRequestProperty("Authorization", "key=" + apiKey);

        conn.setDoOutput(true);

        // 5. Add JSON data into POST request body
        //`5.1 Use Jackson object mapper to convert Contnet object into JSON
        ObjectMapper mapper = new ObjectMapper();

        // 5.2 Get connection output stream
        DataOutputStream wr = new DataOutputStream(conn.getOutputStream());

        // 5.3 Copy Content "JSON" into
        mapper.writeValue(wr, content);

        // 5.4 Send the request
        wr.flush();

        // 5.5 close
        wr.close();

        // 6. Get the response
        int responseCode = conn.getResponseCode();
        System.out.println("\nSending 'POST' request to URL : " + url);
        System.out.println("Response Code : " + responseCode);

        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        // 7. Print result
        System.out.println(response.toString());

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:io.proscript.jlight.Runtime.java

private static Application buildApplication(Environment environment) {
    ClassLoader cl = null;//from   ww  w.  ja va  2s  .  c om
    try {
        cl = ClassLoaderBuilder.build(environment.getCacheDirectory());
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        return new Application(cl, environment.getMainClassName(), "main");
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String parseASX(String inputResource) {
    String inputFile = "";
    try {//from w ww.  j  a  va2 s. c o  m
        String contents = readTextFromUrl(new URL(inputResource));
        for (String line : contents.split("\n")) {
            if (line.toLowerCase().contains("href")) {
                String pattern = "(?i).*href=\"(.*)\".*";
                inputFile = line.replaceAll(pattern, "$1");
                break;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return inputFile;
}

From source file:Main.java

private static String parseXSPF(String inputResource) {
    String inputFile = "";
    try {//from  w ww.  j a  va2s  . c  om
        String contents = readTextFromUrl(new URL(inputResource));
        for (String line : contents.split("\n")) {
            if (line.toLowerCase().contains("href")) {
                String pattern = "(?i)<location>(.*)</location>.*";
                inputFile = line.replaceAll(pattern, "$1");
                break;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    return inputFile;
}