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