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 Bitmap getImageFromURL(String url) {

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

        return getImageFromURL(new URL(url));

    } catch (MalformedURLException e) {

        e.printStackTrace();

    }

    return null;

}

From source file:Main.java

public static Bitmap downLoadBitmap(String httpUrl) {
    InputStream inputStream = null;
    try {/*from   w w w .  ja v a2s. c om*/
        URL url = new URL(httpUrl);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.setReadTimeout(5000);
        conn.setConnectTimeout(5000);
        conn.connect();
        int responseCode = conn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            inputStream = conn.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            return bitmap;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return null;
}

From source file:Main.java

public static Bitmap getBitmap(String uri) {
    try {/* w w w.ja v a2 s. c o m*/
        InputStream is = new URL(uri).openStream();
        return BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

private static String httpGet(String address) {
    InputStream inputStream = null;
    HttpURLConnection httpURLConnection = null;
    try {//w w w . j a v  a2  s .  c  om
        URL url = new URL(address);
        httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = httpURLConnection.getInputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            while ((len = inputStream.read(buffer)) != -1) {
                stringBuffer.append(new String(buffer, 0, len));
            }
            return stringBuffer.toString();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(inputStream);
        if (httpURLConnection != null) {
            httpURLConnection.disconnect();
        }

    }

    return null;

}

From source file:Main.java

public static void post(String actionUrl, String file) {
    try {//from   w  w w . java  2  s . c om
        URL url = new URL(actionUrl);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setDoInput(true);
        con.setDoOutput(true);
        con.setUseCaches(false);
        con.setRequestMethod("POST");
        con.setRequestProperty("Charset", "UTF-8");
        con.setRequestProperty("Content-Type", "multipart/form-data;boundary=*****");
        DataOutputStream ds = new DataOutputStream(con.getOutputStream());
        FileInputStream fStream = new FileInputStream(file);
        int bufferSize = 1024; // 1MB
        byte[] buffer = new byte[bufferSize];
        int bufferLength = 0;
        int length;
        while ((length = fStream.read(buffer)) != -1) {
            bufferLength = bufferLength + 1;
            ds.write(buffer, 0, length);
        }
        fStream.close();
        ds.flush();
        InputStream is = con.getInputStream();
        int ch;
        StringBuilder b = new StringBuilder();
        while ((ch = is.read()) != -1) {
            b.append((char) ch);
        }
        new String(b.toString().getBytes("ISO-8859-1"), "utf-8");
        ds.close();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

private static String httpPost(String address, String params) {
    InputStream inputStream = null;
    HttpURLConnection urlConnection = null;
    try {/*from  ww  w. j  ava  2  s  .c  o  m*/
        URL url = new URL(address);
        urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestMethod("POST");
        urlConnection.getOutputStream().write(params.getBytes());
        urlConnection.getOutputStream().flush();

        urlConnection.connect();
        if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            inputStream = urlConnection.getInputStream();
            int len = 0;
            byte[] buffer = new byte[1024];
            StringBuffer stringBuffer = new StringBuffer();
            while ((len = inputStream.read(buffer)) != -1) {
                stringBuffer.append(new String(buffer, 0, len));
            }
            return stringBuffer.toString();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        close(inputStream);
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
    return null;

}

From source file:com.mobile.godot.util.GodotURLUtils.java

public static URL parseToURL(String servlet, List<BasicNameValuePair> params) {
    String parsedParams = "?";

    for (int count = 0; count < params.size(); count++) {
        String paramName = params.get(count).getName();
        String paramValue = params.get(count).getValue();

        parsedParams += paramName + "=" + paramValue;
        parsedParams += (count + 1 < params.size()) ? "&" : "";
    }/*from   w  w w .ja v  a2 s . c om*/

    String parsedURL = "http://www.godot.mobi/" + servlet + parsedParams;

    try {
        URL url = new URL(parsedURL);
        return url;
    } catch (MalformedURLException exc) {
        exc.printStackTrace();
        return null;
    }
}

From source file:org.wso2.carbon.discovery.cxf.util.ClassAnnotationScanner.java

public static AnnotationDB getAnnotatedClasses(StandardContext context) {

    AnnotationDB db = new AnnotationDB();
    //        db.addIgnoredPackages("org.apache");
    db.addIgnoredPackages("org.codehaus");
    db.addIgnoredPackages("org.springframework");

    final String path = context.getRealPath("/WEB-INF/classes");
    URL resourceUrl = null;/*  w ww  . j  ava 2  s.c  o  m*/
    URL[] urls = null;

    if (path != null) {
        final File fp = new File(path);
        if (fp.exists()) {
            try {
                resourceUrl = fp.toURI().toURL();
                urls = new URL[] { new URL(resourceUrl.toExternalForm()) };
                db.scanArchives(urls);
                return db;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return db;
}

From source file:Main.java

private static URL createURL(String url) {
    URL requestURL;//from  w w  w  . ja  v  a2s .c o  m
    try {
        requestURL = new URL(url);
        return requestURL;
    } catch (MalformedURLException e) {
        try {
            requestURL = new URL("http://localhost:8080" + url);
            return requestURL;
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
    }
    return null;
}

From source file:Main.java

/**
 * This method checks if the Network available on the device or not.
 * /*  w w w. j a  v a  2s .com*/
 * @param context
 * @return true if network available, false otherwise
 */
public static Boolean isNetworkAvailable(Context context) {
    boolean connected = false;
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        connected = true;
    } else if (netInfo != null && netInfo.isConnected() && cm.getActiveNetworkInfo().isAvailable()) {
        connected = true;
    } else if (netInfo != null && netInfo.isConnected()) {
        try {
            URL url = new URL("http://www.google.com");
            HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
            urlc.setConnectTimeout(3000);
            urlc.connect();
            if (urlc.getResponseCode() == 200) {
                connected = true;
            }
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else if (cm != null) {
        final NetworkInfo[] netInfoAll = cm.getAllNetworkInfo();
        for (NetworkInfo ni : netInfoAll) {
            System.out.println("get network type :::" + ni.getTypeName());
            if ((ni.getTypeName().equalsIgnoreCase("WIFI") || ni.getTypeName().equalsIgnoreCase("MOBILE"))
                    && ni.isConnected() && ni.isAvailable()) {
                connected = true;
                if (connected) {
                    break;
                }
            }
        }
    }
    return connected;
}