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 getParameterFromUrl(String url, String param) {
    //Log.d(TAG, "getParameterFromUrl:"+url+"-p:"+param);
    URL iurl;/*from ww  w.ja v  a  2 s.c  om*/
    try {
        iurl = new URL(url);
        String iquery = iurl.getQuery();
        if (iquery == null) {
            return "";
        } else {
            String[] q = iquery.split("&");
            for (int i = 0; i < q.length; i++) {
                String[] xx = q[i].split("=");
                if (xx[0].equals(param) && xx.length > 1) {
                    return xx[1];
                }
            }
            return "";
        }

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

From source file:Main.java

public static InputStream downloadFromURL(String urlString) {
    InputStream retval = null;/* w w  w .  ja  va2s. com*/
    try {
        URL url = new URL(urlString);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        //URLConnection ucon = url.openConnection();
        con.setRequestMethod("GET");
        con.setDoInput(true);
        retval = con.getInputStream();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return retval;
}

From source file:Main.java

public static Bitmap decodeBitmapFromURL(String src, boolean large) {

    // If the artwork returned null, don't want to try to show artwork
    if (src.equals("null")) {
        return null;
    }/*  w w w  .  j  a v a  2s. com*/

    if (large) {
        src = src.replace("large", "t500x500");
    }

    InputStream inputStream = null;
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        inputStream = connection.getInputStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (inputStream == null) {
        return null;
    }

    // Decided not to scale because would have to recreate input stream
    /*
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(inputStream, null, options);
            
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
            
    options.inJustDecodeBounds = false; */
    Bitmap returnBitmap = BitmapFactory.decodeStream(inputStream);
    return returnBitmap;
}

From source file:Main.java

/**
 * Makes a GET call to the server./*www.  j a v a  2s  . co  m*/
 * @return The serve response (expected is JSON)
 */
public static String httpGet(String urlStr) {
    try {
        URL url = new URL(urlStr);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        String r = readStream(in);
        urlConnection.disconnect();
        return r;
    } catch (MalformedURLException e) {
        Log.v("MyLog", "GET: Bad URL");
        e.printStackTrace();
        return "GET: Bad URL";
    } catch (IOException e) {
        Log.v("MyLog", "GET: Bad Con");
        e.printStackTrace();
        return "GET: Bad Con [" + urlStr + "]";
    }
}

From source file:Main.java

/**
 * Downloads a file via HTTP(S) GET to the given path. This function cannot be called from the
 * UI thread. Android does not allow it.
 *
 * @param urlString Url to the ressource to download.
 * @param file      file to be written to.
 * @param overwrite if file exists, overwrite?
 * @return flase if download was not successful. If successful, true.
 *//*from  www. j a  va2  s  .com*/
private static Boolean fileDownloadHttp(String urlString, File file, Boolean overwrite) {
    HashMap<String, String> result = null;
    URL url = null;
    //        File temp;

    try {
        url = new URL(urlString);

        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setReadTimeout(200000);
        urlConnection.connect();
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());

        FileOutputStream outputStream = new FileOutputStream(file);

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = in.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        in.close();
        outputStream.close();
        urlConnection.disconnect();
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    Log.d(TAG, "File download: " + file.getAbsolutePath() + url.getFile() + "overwrite " + overwrite
            + "exists? " + file.exists());
    return true;
}

From source file:Main.java

public static InputStream getCommunityPicInputStream(String s) {
    InputStream inputstream = null;
    try {//from   w w w  .j  a v a 2  s.  c  o m
        inputstream = (new URL(
                (new StringBuilder()).append("http://service.itouchchina.com/").append(s).toString()))
                        .openConnection().getInputStream();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return inputstream;

}

From source file:Main.java

public static String uploadFile(String filePath, String requestURL) {

    String result = "";
    File file = new File(filePath);

    try {//from   ww  w. ja  va2s.  co m
        HttpURLConnection connection = initHttpURLConn(requestURL);

        if (filePath != null) {
            DataOutputStream dos = new DataOutputStream(connection.getOutputStream());
            StringBuffer sb = new StringBuffer();
            InputStream is = new FileInputStream(filePath);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                dos.write(bytes, 0, len);
            }
            dos.flush();
            is.close();
            int res = connection.getResponseCode();
            Log.e(TAG, "response code:" + res);
            if (res == 200) {
                Log.e(TAG, "request success");
                InputStream input = connection.getInputStream();
                StringBuffer sb1 = new StringBuffer();
                int ss;
                while ((ss = input.read()) != -1) {
                    sb1.append((char) ss);
                }

                result = sb1.toString();
                Log.d(TAG, "result: " + result);

                input.close();
            }
        }

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

    return result;

}

From source file:com.netsteadfast.greenstep.util.WsServiceUtils.java

public static boolean testConnection(String wsdlAddress) throws Exception {
    if (StringUtils.isBlank(wsdlAddress)) {
        return true;
    }/*from   www. ja  v a2 s  .c  om*/
    boolean status = false;
    try {
        URL url = new URL(wsdlAddress);
        URLConnection connection = url.openConnection();
        connection.setConnectTimeout(TIMEOUT);
        connection.setReadTimeout(TIMEOUT);
        if (connection.getContent() != null) {
            status = true;
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return status;
}

From source file:com.github.walterfan.util.http.URLHelper.java

public static String getPathAndQuery(String strUrl) {
    try {/*from  w  ww. j a v  a  2s  .  c om*/
        URL url = new URL(strUrl);
        //System.out.println("url=" + url);
        String path = url.getPath();
        String query = url.getQuery();
        if (StringUtils.isNotBlank(query)) {
            path = path + "?" + query;
        }
        if (path.startsWith("/")) {
            return path.substring(1);
        }

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

    }
    return "";
}

From source file:Main.java

public static String uploadFile(File file, String RequestURL) {
    String BOUNDARY = UUID.randomUUID().toString();
    String PREFIX = "--", LINE_END = "\r\n";
    String CONTENT_TYPE = "multipart/form-data";

    try {/*from www.  ja  v a2  s  .c  om*/
        URL url = new URL(RequestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(TIME_OUT);
        conn.setConnectTimeout(TIME_OUT);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Charset", CHARSET);
        conn.setRequestProperty("connection", "keep-alive");
        conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
        if (file != null) {

            OutputStream outputSteam = conn.getOutputStream();

            DataOutputStream dos = new DataOutputStream(outputSteam);
            StringBuffer sb = new StringBuffer();
            sb.append(PREFIX);
            sb.append(BOUNDARY);
            sb.append(LINE_END);

            sb.append("Content-Disposition: form-data; name=\"img\"; filename=\"" + file.getName() + "\""
                    + LINE_END);
            sb.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END);
            sb.append(LINE_END);
            dos.write(sb.toString().getBytes());
            InputStream is = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                dos.write(bytes, 0, len);
            }
            is.close();
            dos.write(LINE_END.getBytes());
            byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes();
            dos.write(end_data);
            dos.flush();

            int res = conn.getResponseCode();
            Log.e(TAG, "response code:" + res);
            if (res == 200) {
                return SUCCESS;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return FAILURE;
}