Come on, you know you want to buy me a beer!

I've just had a very interesting problem, one that's been bothering me for a few days now.
I had assumed that when retrieving XML from a remote URL in Android Java, you'd automatically get the new lines output. Well, I was wrong.

For some reason SAX or converting to String was stripping new lines, which meant all my sentences were being combined into one.

Here's how I fixed it.

First, in PHP add this before outputting a value:

Code:
1
2
3
4
5
<?php

$finalString 
str_replace(PHP_EOL,"\\n",$string);


Now, in Android I'm retrieving the XML something like this:

Code:
1
2
3
4
5
6
7
8
9
10
11
HttpResponse Content = Client.execute(httppost);
HttpResponseText = inputStreamToString(Content.getEntity().getContent());

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
                
VerifyResponseHandler lrh = new VerifyResponseHandler();
                
InputStream xmlStream = new ByteArrayInputStream(HttpResponseText.getBytes("UTF-8"));


You might be wondering what's actually going on, and I'm not going to go into the process of calling a URL to retrieve XML, but basically, I was using the String value of the response by running it through a function called inputStreamToString

Here's how the function inputStreamToString looks:

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
        private String inputStreamToString(InputStream is) throws IOException {
            String str = "";
            String line = "";
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            while ((line = rd.readLine()) != null) { str += line; }
            
            StringBuffer sb = new StringBuffer(str.length());

            CharacterIterator it = new StringCharacterIterator(str);
            for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
                switch (ch) {
                    case '\\':
                        char next = str.charAt(it.getIndex() + 1);
                        if (next == 'n') {
                            // use new line character
                            sb.append('\n');
                        } else {
                            sb.append(ch);
                        }
                        break;
                    case 'n':
                        char prev = str.charAt(it.getIndex() - 1);
                        if (prev == '\\') {
                            // do nothing
                        } else {
                            sb.append(ch);
                        }
                        break;
                    default:
                        sb.append(ch);
                        break;
                }
            }
            
            str = sb.toString();
            
            return str;
        }


Now, drop that into a TextView and you'll see the new lines.

As with any programming problems, there's always a solution, but sometimes understanding where you're going wrong helps. I went wrong with converting the response to a string first, but unfortunately I'm too far into this to stop. It needs to get put on the Market ASAP.

Comments


Your Name:

Email Address: (won't be published)

Comments: (no HTML allowed)

 

There are no comments yet.

 

 

 

 

 

 

Latest Posts