Android does not interpret command " n" of a web page

Asked

Viewed 173 times

0

I’m creating an app that it accesses a URL page, query some data, and returns the result in a Alert dialog, however in the web code, made in PHP, I need to put some " n" to format the data, and everything that there is before the " n" Android does not interpret, ie if the result has:

Answer, "n", answer2, Android can only read the "answer2", did anyone have the same problem? Below follows my Android code that performs the query and generates the answer.

URLConnection con = url.openConnection();
con.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());

writer.write("nome="+nomeProduto+"&aliquota="+aliquota+(laboratorio != null ? "&laboratorio="+laboratorio : ""));
writer.flush();

String line;

StringBuffer result=new StringBuffer();

BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));

while ((line = reader.readLine()) != null) {

        alertDialog.setMessage(line);
        alertDialog.setButton("Voltar",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int id) {
                dialog.cancel();
            }});

        alertDialog.show();
}

writer.close();
reader.close();
  • 2

    The problem is you’re making one readLine() which reads basically until you find ' n'.

1 answer

1

Do so:

String result="";
while((line = br.readLine())!=null){
     result += line;
}

alertDialog.setMessage(result);
alertDialog.setButton("Voltar",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            dialog.cancel();
        }});
alertDialog.show();

That way you’ll read all the lines.

  • Juarez, he really recognized it! but in Alert dialog it le as if it were a single line string, and in the test url it divides the lines normally, is possible android recognize exactly as is on the web or have to sort the data directly on mobile?

  • 1

    Guy on android he recognizes the \n Maybe in line reading he won’t discard the \n then I suggest you re-fix the \n as follows result+=(line+"\n");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.