Partial URL + Uri.parse

1

I am faced with a problem trying to add a partial URL + a domain or URL generated by an EditText, I leave an example of the code if there could be one telling me where the error is, since I can not find a solution by any means.

Code:

Uri uriUrl = "http://google"+Uri.parse(et1.getText().toString());
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);

So I understood that it was written in a thousand ways but I can not find the correct way

    
asked by Franco Galuzzi 16.11.2016 в 06:27
source

2 answers

4

The problem is that you have to convert all your url which is a String to Uri , using Uri.parse() :

Uri uriUrl = Uri.parse("http://google"+et1.getText().toString());

in this way use it in the Intent:

    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
startActivity(launchBrowser);

This is another option:

    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google"+et1.getText().toString()));
startActivity(launchBrowser);

Be sure to check that the url you are trying to convert to Uri , is actually valid:

String miUrl = "http://google"+et1.getText().toString();
    
answered by 16.11.2016 / 13:00
source
1

In this line you are trying to make a Uri.parse to only a text that can be .com , .es , .cl , etc etc.

What I recommend is that before trying to do that, check if what has the EditText is a URL. Also what you could do is define a array with the domains that you intend to accept.

String[] dominios = { ".com", ".es", ".cl", ".pe"};
if(URLUtil.isValidUrl(et1.getText().toString()){
    Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse(et1.getText().toString()));
    startActivity(intent);
}else{
    //Si no es una URL formas la que tienes de ejemplo
    if (Arrays.asList(dominios).contains(et1.getText().toString())) {
       Uri uriUrl = Uri.parse("http://www.google"+et1.getText().toString());
       Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
       startActivity(intent);
    }else{
       //Error el dominio ingresado no esta.
    }
}
    
answered by 16.11.2016 в 12:39