1

I am using following code to obtain some data from my website using android HttpURLConnection method and place it on webview textarea.But unfortunately the obtained data ignores all newlines and empty lines. Could an expert show me how i can keep the empty lines and new lines on my final result ?Thanks in advance.

test.Data.php data:

helloWorld1

next2
next3

output:

helloWorld1next2next3  

expected output:

helloWorld1

next2
next3

Android code:

public String sendGetRequest() {

        try {

            StrictMode.setThreadPolicy(new Builder().permitAll().build());

            HttpURLConnection myURLConnection = (HttpURLConnection) new URL("http://mywebsite.com/testData.php").openConnection();
            myURLConnection.setReadTimeout(60000);
            myURLConnection.setConnectTimeout(60000);
            myURLConnection.setRequestMethod("GET");
            myURLConnection.setUseCaches(false);
            myURLConnection.setDoInput(true);
            myURLConnection.setDoOutput(true);
            myURLConnection.setRequestProperty("Content-Type", "text");
            myURLConnection.setRequestProperty("User-Agent", "Dalvik/2.1.0 (Linux; U; Android 6.0;");

            OutputStream os = myURLConnection.getOutputStream();

            os.close();

            myURLConnection.connect();

            BufferedReader in = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));


            StringBuffer sb = new StringBuffer();


            String line;

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

                sb.append(line);

            }
            in.close();


            // Update your UI
            mWebView.loadUrl("javascript:Parseit('" + sb.toString() + "');");


        } catch (Exception e) {

        }
        return null;

    }

javascript on webview:

function Parseit(data)
{

var myTextArea = document.getElementById('area1');
myTextArea.innerHTML += data+"\n";

};
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
user1788736
  • 2,727
  • 20
  • 66
  • 110

1 Answers1

3

Add a breakline while you are appending the string :

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

                sb.append(line);
                sb.append("\n");
            }
diegoveloper
  • 93,875
  • 20
  • 236
  • 194
  • Thanks for reply. I already tried that and it didn't work. I read in other posts that newline and empty line are lost when using readline()!But i don't know how to preserve them! – user1788736 Nov 22 '17 at 04:23
  • what is your result using this approach? because as you see the code , it's adding a new break line in every line that you read. Maybe your server response is just one line ? – diegoveloper Nov 22 '17 at 04:26
  • If it type the url of testData.php on the browser the sample data shows as expected in multiple lines (same as above expected output) so the server response can't be one line.Even if i look at source code of testData.php the data is multiple lines! – user1788736 Nov 22 '17 at 04:36
  • ok and what is the result using this approach? Could you print every line inside the while? Log.d("tag", line); sb.append(line); – diegoveloper Nov 22 '17 at 04:36
  • result using sb.append(line);sb.append("\n"); is like this :helloWorld1
    next2
    next3. I have to mention that i used different approach by adding
    manually to my sever data(testData.php) to see if that makes a difference but still all in one line!(if i don't put those
    and only type the data on different lines then when i type the url on browser all the data shows in one line!) .So sever data with or without
    produces all data on one line inside webview textarea!
    – user1788736 Nov 22 '17 at 04:44
  • try to add \n instead of
    in your server
    – diegoveloper Nov 22 '17 at 04:45
  • After putting \n per your suggestion it output the data on multiple lines! But in the future if i want to parse any data that is not on my server then i will face lots of problem parsing that remote content!(Another case is if i want to parse a remote html how i should deal with that case because i can't modify the remote content by adding \n at the end of each line!) – user1788736 Nov 22 '17 at 04:58
  • The server should not return plain text, it should returns json or xml content. – diegoveloper Nov 22 '17 at 04:59
  • Then how we can parsing remote m3u playlist since it is not xml or json ? – user1788736 Nov 22 '17 at 23:27
  • It's an example man, what do you need? The server is in charge to send the data correctly – diegoveloper Nov 22 '17 at 23:29