ponnamkarthik / RichLinkPreview

A Rich Link Preview Library for Android
Apache License 2.0
217 stars 58 forks source link

unable to handle MalformedException #20

Open vishalvishvakarma opened 4 years ago

vishalvishvakarma commented 4 years ago

I m getting this error and i am unable to handle it plz help how to handle it i have tried my code with try catch but exception is occur in
your side which i cannot handle

java.lang.RuntimeException: An error occured while executing doInBackground() java.lang.IllegalArgumentException: Malformed URL java.net.MalformedURLException: Unknown protocol

Ankur008 commented 4 years ago

@vishalvishvakarma did you got any solution?

vishalvishvakarma commented 4 years ago

I clone this project and added all the classes in my project and handle the exception Which i m getting @ankur008

ririlya94 commented 4 years ago

@vishalvishvakarma @Ankur008 you can try this way, it prevents the app from crashing

 ArrayList<String> containedUrls = new ArrayList();
            Pattern pattern = Pattern.compile("(?<protocol>(http|ftp|https|ftps):\/\/)?(?<site>[\w\-_\.]+\.(?<tld>([0-9]{1,3})|([a-zA-Z]{2,3})|(aero|arpa|asia|coop|info|jobs|mobi|museum|name|travel))+(?<port>:[0-9]+)?\/?)((?<resource>[\w\-\.,@^%:/~\+#]*[\w\-\@^%/~\+#])(?<queryString>(\?[a-zA-Z0-9\[\]\-\._+%\$#\~',]*=[a-zA-Z0-9\[\]\-\._+%\$#\~',]*)+(&[a-zA-Z0-9\[\]\-\._+%\$#\~',]*=[a-zA-Z0-9\[\]\-\._+%\$#\~',]*)*)?)?");
            Matcher urlMatcher = pattern.matcher("<your url link here>");

            while (urlMatcher.find()) {
                containedUrls.add(
                        "".substring(
                        urlMatcher.start(0),
                        urlMatcher.end(0)
                ));
            }

//then we check if contains url we are going to assign richlinkpreview
            if(containedUrls.size() > 0)
            {
//let's try again if it's url is valid
                try {
                    URL url = new URL(containedUrls.get(0));
                } catch (MalformedURLException e) {
                    System.out.println("The URL is not valid.");
                    System.out.println(e.getMessage());
                    return;
                }

richLinkView.setLink("https://stackoverflow.com", new ViewListener() {
                @Override
                public void onSuccess(boolean status) {}
                @Override
                public void onError(Exception e) {}
            });

            }
PeggTime commented 4 years ago

@ririlya94 what is the value of containedUrls arraylist? In the line number 3(

Matcher urlMatcher = pattern.matcher("");

), instead of "" , url value will come here?

ririlya94 commented 4 years ago

@ririlya94 what is the value of containedUrls arraylist? In the line number 3(

Matcher urlMatcher = pattern.matcher("");

), instead of "" , url value will come here?

@PeggTime yes correct. Sorry for the confusion there.