RichLinkPreview icon indicating copy to clipboard operation
RichLinkPreview copied to clipboard

unable to handle MalformedException

Open vishalvishvakarma opened this issue 4 years ago • 5 comments

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

vishalvishvakarma avatar Jun 11 '20 11:06 vishalvishvakarma

@vishalvishvakarma did you got any solution?

Ankur008 avatar Jun 16 '20 12:06 Ankur008

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

vishalvishvakarma avatar Jun 16 '20 12:06 vishalvishvakarma

@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) {}
            });


            }

ririlya94 avatar Aug 06 '20 04:08 ririlya94

@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 avatar Aug 25 '20 04:08 PeggTime

@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.

ririlya94 avatar Aug 25 '20 06:08 ririlya94