Split (), using | (pipe) as a separator, does not separate the text correctly

4

I'm developing an app for android and I've now done the part of HttpURLConnection , put everything right, pointed to the url and did a test with% my string with all data coming from my hosted php file.

private class MyAsyncTask extends  AsyncTask
{
    @Override
    protected Object doInBackground(Object[] params)
    {
        HttpURLConnection conection;
        try
        {
            //Configuração de conexao
            URL url = new URL("http://prinxel.esy.es/horoscopo.php");
            conection = (HttpURLConnection) url.openConnection();
            conection.connect();

            // Lendo os dados

            InputStream inputStream = conection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuffer stringBuffer = new StringBuffer();

            //Storing data

            String line = new String();

            while ((line = bufferedReader.readLine())!= null)
            {
                System.out.println(line); // imprime todos os dados certinhos
                textoSeparado = line.split("|"); 
            }

            //Close Conection
            inputStream.close();
            conection.disconnect();

        }

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

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


        return  null;
    }

    @Override
    protected void onPostExecute(Object o)
    {
        super.onPostExecute(o);
    }
}

However, when I use split to separate the items from my string, nothing appears on my console. I want to split to put a array message in each text view that will be formed after the split of variable System.out.printl(line) .

Variable content line

|Gêmeos vai sentir-se com algumas limitações que serão criadas apenas pela sua cabeça já que é um mês em que terá progresso e evolução.|Câncer estará bastante empenhado e concentrado no seu trabalho podendo esquecer-se por vezes da sua vida afetiva.|Leão terá um mês positivo em que poderá obter a realização que deseja em vários setores da sua vida.|

ps: I already tried to line out of while and did not work

    
asked by anonymous 28.05.2016 / 21:34

1 answer

4

split () receives as parameter one regex , as | has a special function (it's a meta-character) in regex it is not being considered as a separator character.

To ensure that | is properly interpreted, you need to be "escaped":

textoSeparado = line.split("\|");

Another way to treat meta-characters is to use Pattern.quote ()

textoSeparado = line.split("Pattern.quote("|"));
    
29.05.2016 / 11:24