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:cc.kune.core.server.utils.UrlUtils.java

/**
 * Of.//from  w  w  w  .  j  a  v  a 2 s .  c o m
 * 
 * @param urlString
 *          the url string
 * @return the url
 */
public static URL of(final String urlString) {
    URL url = null;
    try {
        url = new URL(urlString);
        return url;
    } catch (final MalformedURLException e) {
        LOG.error("Error creating url with" + urlString);
        e.printStackTrace();
    }
    return url;
}

From source file:Main.java

/**
 * Parses the PLS file and returns the first file name.
 * //from w w  w  .  j a  v  a 2 s.  c o  m
 * @param inputUrl
 *            The input PLS file.
 * @return The first file name in the PLS playlist.
 */
public static String parsePLS(String inputUrl) {
    String inputFile = "";
    try {
        String plsContents = readTextFromUrl(new URL(inputUrl));
        for (String line : plsContents.split("\n")) {
            if (line.startsWith("File1=")) {
                inputFile = line.replace("File1=", "").trim();
                break;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    return inputFile;
}

From source file:com.appdynamics.common.RESTClient.java

public static void sendGet(String urlString, String apiKey) {

    try {//from  ww  w  . j  av  a  2  s  .  c om

        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Authorization",
                "Basic " + new String(Base64.encodeBase64((apiKey).getBytes())));

        if (conn.getResponseCode() != 200) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        logger.info("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            logger.info(output);
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:com.appdynamics.common.RESTClient.java

public static void sendPost(String urlString, String input, String apiKey) {
    try {/*  www.  j a  v a 2 s  .c o m*/
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");

        conn.setRequestProperty("Authorization",
                "Basic " + new String(Base64.encodeBase64((apiKey).getBytes())));

        OutputStream os = conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

        String output;
        logger.info("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            logger.info(output);
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

From source file:Main.java

/**
 * Return an {@link InputStream} from the given url or null if failed to retrieve the content
 * //w w  w .j  a  v  a  2  s .  co m
 * @param uri
 * @return
 */
static InputStream openRemoteInputStream(Uri uri) {
    java.net.URL finalUrl;
    try {
        finalUrl = new java.net.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;
    }

    // permanent redirection
    if (code == HttpURLConnection.HTTP_MOVED_PERM || code == HttpURLConnection.HTTP_MOVED_TEMP
            || code == HttpURLConnection.HTTP_SEE_OTHER) {
        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:fuliao.fuliaozhijia.data.UserData.java

public static String download(String mediaId, String dir) {
    //      http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID
    URL source;// www .j  ava2 s  .  c  o  m
    try {
        source = new URL("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="
                + "oPaVk_NjH_TCX1fQhIVz_8DoRqE85Vx2E_sawJWQXpen5Q6HykjRnqA--6yE-y2VaQTU1f3vY5K-udylcgm55igkwa--7kVQ-KyDndcylmE"
                //WeixinAccessUtil.getAccessToken()
                + "&media_id=" + mediaId);
        URLConnection connection = source.openConnection();
        connection.setConnectTimeout(60 * 1000);
        connection.setReadTimeout(300 * 1000);
        InputStream input = connection.getInputStream();
        String fileType = connection.getHeaderField("Content-disposition");
        System.out.println("Content-disposition:" + fileType);
        String fileName = UUID.randomUUID().toString()
                + fileType.substring(fileType.lastIndexOf("."), fileType.length() - 1);
        File file = new File(dir + fileName);
        FileUtils.copyInputStreamToFile(input, file);
        return fileName;
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:biomine.bmvis2.crawling.Databases.java

public static Collection<String> getDatabases() {
    if (dbs != null)
        return dbs;

    try {/*  ww  w .  jav a2s. c o m*/
        String url = WebConstants.BIOMINE_URL + "stats/index.cgi?json_action=getdbs";
        String cont = URLUtils.getURLContents(new URL(url));
        Object arr = JSONValue.parse(cont);
        if (arr instanceof JSONArray) {
            JSONArray jarr = (JSONArray) arr;
            dbs = new ArrayList();
            for (Object dbo : jarr) {
                JSONObject jdb = (JSONObject) dbo;
                Object no = jdb.get("name");
                if (no != null)
                    dbs.add(no.toString());
            }
            Collections.reverse(dbs);
            return dbs;
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassCastException e) {
        // TODO: handle exception
    }
    return Collections.EMPTY_LIST;
}

From source file:com.ibm.watson.WatsonVRTraining.util.images.PhotoCaptureFrame.java

public static void updateCaptureFrame(File capturedImgFile, String img_result_html) {

    Photo photo = null;/*from w w w  .  j a  v  a 2s.c  o m*/
    try {
        photo = new Photo("IBM Watson predictions for below image", capturedImgFile.toURI().toURL(),
                img_result_html);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    PhotoCaptureFrame.getPhotoesJPanel().add(photo);
    PhotoCaptureFrame.getPhotoesJFrame().repaint();
    PhotoCaptureFrame.getPhotoesJFrame().setVisible(true);
}

From source file:Main.java

public static Bitmap urlToBitmap(String siteUrl, int requireSize) {
    //Log.d(LOG_TAG, "call the urlToBitmap " + ++callTime); // called 8 times..

    if (siteUrl == null) {
        //Log.d(LOG_TAG, "the url is null, throw Exception");
        return null;
    }/*from   w w w . j  a  va 2 s  . co m*/

    URL url;
    try {
        url = new URL(siteUrl);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(url.openStream(), null, options);

        // determinte the scale size
        int scale = 1;
        //Log.d(Constant.LOG_TAG, "the outHeight is " + options.outHeight + ", the outWidth is " + options.outWidth);

        scale = computeSampleSize(options, -1, requireSize * requireSize);
        options.inJustDecodeBounds = false;
        //Log.d(Constant.LOG_TAG, "the scale is " + scale);
        options.inSampleSize = scale;
        Bitmap bitmap = BitmapFactory.decodeStream(url.openStream(), null, options);
        //Log.d(LOG_TAG, "get the bitmap in urltobitmap " + callTime);
        return bitmap;
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //Log.d(LOG_TAG, "return null in urltobitmap " + callTime);
    return null;

}

From source file:Main.java

public static void server2mobile(Context context, String type) {

    String serverPath = "http://shaunrain.zicp.net/FileUp/QueryServlet?type=" + type;
    try {//w w w . j  a v a  2 s  .  com
        URL url = new URL(serverPath);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(10000);
        conn.setRequestMethod("GET");

        int code = conn.getResponseCode();
        Log.d("code", code + "");
        if (code == 200) {
            InputStream is = conn.getInputStream();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int len = 0;
            byte[] buffer = new byte[1024];

            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            is.close();
            conn.disconnect();

            String result = new String(baos.toByteArray());
            String[] results = result.split("[$]");
            for (String r : results) {
                if (r.length() != 0) {
                    URL json = new URL(r);
                    HttpURLConnection con = (HttpURLConnection) json.openConnection();
                    con.setConnectTimeout(5000);
                    con.setRequestMethod("GET");
                    int co = con.getResponseCode();
                    if (co == 200) {
                        File jsonFile;
                        if (r.endsWith("clear.json")) {
                            jsonFile = new File(
                                    Environment.getExternalStorageDirectory() + "/traffic_json/clear.json");
                        } else if (r.endsWith("crowd.json")) {
                            jsonFile = new File(
                                    Environment.getExternalStorageDirectory() + "/traffic_json/crowd.json");
                        } else if (r.endsWith("trouble.json")) {
                            jsonFile = new File(
                                    Environment.getExternalStorageDirectory() + "/traffic_json/trouble.json");
                        } else {
                            jsonFile = new File(
                                    Environment.getExternalStorageDirectory() + "/traffic_json/control.json");
                        }

                        if (jsonFile.exists())
                            jsonFile.delete();
                        jsonFile.createNewFile();
                        try (BufferedReader reader = new BufferedReader(
                                new InputStreamReader(con.getInputStream(), "gb2312"));
                                BufferedWriter writer = new BufferedWriter(new FileWriter(jsonFile));) {
                            String line = null;
                            while ((line = reader.readLine()) != null) {
                                writer.write(line);
                            }
                        }

                    }
                    con.disconnect();
                }
            }

        }

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

}