0

I am doing a dictionary App and i am using this xml url.

While parsing this url I am getting OutOfMemoryError. Can anybody please tell me why it's coming and what is the correct way to solve this problem? And please give me some other idea to do dictionary App.

Here is my code what i am using for this XML parsing:

public class MainActivity extends Activity {
    TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);

        text = (TextView) findViewById(R.id.textView1);

        try {
            parse();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    void parse() throws IOException, XmlPullParserException {

        URL url = new URL("http://dumps.wikimedia.org/enwiktionary/latest/enwiktionary-latest-abstract.xml");

        // Create a new instance of a PullParserFactory that can be used to
        // create XML pull parsers
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

        // Specifies that the parser produced by this factory will provide
        // support for XML namespaces. By default the value of this is set to
        // false.
        factory.setNamespaceAware(true);

        // Creates a new instance of a XML Pull Parser using the currently
        // configured factory features
        XmlPullParser xpp = factory.newPullParser();

        // Sets the input stream the parser is going to process. This call
        // resets the parser state and sets the event type to the initial value
        // START_DOCUMENT.
        xpp.setInput(url.openStream(), null);

        // Returns the type of the current event (START_TAG, END_TAG, TEXT,
        // etc.)
        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                // text.append(xpp.getName());
            } else if (eventType == XmlPullParser.TEXT) {

                text.append(xpp.getText());
            } else if (eventType == XmlPullParser.END_TAG) {

                // text.append(xpp.getName());
            }
            eventType = xpp.next();
        }
    }
}

Please help...Thanks

2
  • That file is 1.7 gigabytes, you can't load it onto a phone so easily. Commented Feb 22, 2014 at 6:32
  • Then suggest me how to achieve this....I am new in android plzz tell me the easiest way or any simple way to develop an dictionary App. Commented Feb 22, 2014 at 6:45

1 Answer 1

3

Well, the answer to "why" is easy: the file is 1.7G in size, and you try to store it all in memory.

To solve this problem, you have to change the way you are working: instead of parsing the whole file, store it in a database on a server, and query the server when you need a word. You can also try to store only the words in your application (in a compressed structure, like a DAWG), and get the definitions "on the fly" from your server when the user requests them.

Sign up to request clarification or add additional context in comments.

2 Comments

I am new in android. So, can you plz tell me how to do all these things... i want to do it plz
The only magic words you are expecting to read to know all those things you want, are waiting for you in any "Android Book" you decide to read...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.