mohan
mohan

Reputation: 13165

How to avoid out of memory error in android

I am getting the value from webservice while unescape xml value. I am storing it in a stringBuffer. I am getting a lot of values from webservice. So I am getting out of memory error. Can anybody tell me how to avoid that?

My UnescapeXml code:

public  String unescapeXML(String str) {
            if (str == null || str.length() == 0)
              return "";

            StringBuffer buf = new StringBuffer();
            int len = str.length();
            for (int i = 0; i < len; ++i) {
              char c = str.charAt(i);
              if (c == '&') {
                int pos = str.indexOf(";", i);
                if (pos == -1) { // Really evil
                  buf.append('&');
                } else if (str.charAt(i + 1) == '#') {
                  int val = Integer.parseInt(str.substring(i + 2, pos), 16);
                  buf.append((char) val);
                  i = pos;
                } else {
                  String substr = str.substring(i, pos + 1);
                  if (substr.equals("&amp;"))
                    buf.append('&');
                  else if (substr.equals("&lt;"))
                    buf.append('<');
                  else if (substr.equals("&gt;"))
                    buf.append('>');
                  else if (substr.equals("&quot;"))
                    buf.append('"');
                  else if (substr.equals("&apos;"))
                    buf.append('\'');
                  else if (substr.equals("&nbsp;"))
                      buf.append(" ");

                  else
                    // ????
                    buf.append(substr);
                  i = pos;
                }
              } else {
                buf.append(c);
              }
            }
            return buf.toString();
          }

Thanks

Upvotes: 0

Views: 940

Answers (2)

samy
samy

Reputation: 324

Not sure if this is what you are doing but it actually looks like you are simply replacing the html entities with the according character. There are already functions provided in the Document class. You might be looking for normalizeDocument(). You should really use streams instead of strings in this case.

Document Class

Upvotes: 0

Al Sutton
Al Sutton

Reputation: 3924

You should switch to streaming the data and unescaping it as part of a custom Stream or Reader class as opposed to trying to read everything at once.

Upvotes: 1

Related Questions