• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps SAX XML giving strange error

lazure

Lurker
Hi!
Im trying to code an application that reads an external XML file and returns it nicely. This is thanks to lots of tutorials working now, but I have a strange problem that seems to occur when the application has been idle for some time (and not closed) and then is refreshed.

On my phone it's simply getting stuck for 30 seconds and then throws me a force close. On the emulator, (I think this is the same problem) I get the following error:

Code:
04-07 13:12:11.611: ERROR/myDebugTag(2187): myQueryError
04-07 13:12:11.611: ERROR/myDebugTag(2187): org.apache.harmony.xml.ExpatParser$ParseException: At line 1, column 0: no element found
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at org.apache.harmony.xml.ExpatParser.finish(ExpatParser.java:553)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:483)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:320)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:277)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.funrockmedia.stat.main.refreshData(main.java:68)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.funrockmedia.stat.main.onOptionsItemSelected(main.java:38)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at android.app.Activity.onMenuItemSelected(Activity.java:2195)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at android.view.View$PerformClick.run(View.java:8816)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at android.os.Handler.handleCallback(Handler.java:587)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at android.os.Looper.loop(Looper.java:123)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at java.lang.reflect.Method.invokeNative(Native Method)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at java.lang.reflect.Method.invoke(Method.java:521)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-07 13:12:11.611: ERROR/myDebugTag(2187):     at dalvik.system.NativeStart.main(Native Method)
04-07 13:12:12.314: WARN/InputManagerService(71): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44ec9e28
04-07 13:12:23.851: DEBUG/dalvikvm(164): GC_FOR_MALLOC freed 11966 objects / 524152 bytes in 81ms
04-07 13:13:27.934: DEBUG/SntpClient(71): request time failed: java.net.SocketException: Address family not supported by protocol

This is my code:
Code:
public class main extends Activity {
	private final String MY_DEBUG_TAG = "myDebugTag";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        refreshData();
    }
    
    public boolean onCreateOptionsMenu(Menu menu){
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    public boolean onOptionsItemSelected (MenuItem item){
    	switch (item.getItemId()){
    		case R.id.refresh:
    			// Refresh
    			refreshData();
    			return true;
    		case R.id.exit:
    			// Exit
    			this.finish();
    			return true;
    	}
    	return false;
	}
    
    public void refreshData(){
        setContentView(R.layout.main);
        TextView fillText = (TextView)findViewById(R.id.txtStats); 
        fillText.setText("Loading..");
        
        try {
            /* Create a URL we want to load some xml-data from. */
            URL url = new URL("http://...");

            /* Get a SAXParser from the SAXPArserFactory. */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();

            /* Get the XMLReader of the SAXParser we created. */
            XMLReader xr = sp.getXMLReader();
            /* Create a new ContentHandler and apply it to the XML-Reader*/
            ExampleHandler myExampleHandler = new ExampleHandler();
            xr.setContentHandler(myExampleHandler);
           
            /* Parse the xml-data from our URL. */
            xr.parse(new InputSource(url.openStream()));
            /* Parsing has finished. */

            /* Our ExampleHandler now provides the parsed data to us. */
            ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();

            /* Set the result to be displayed in our GUI. */
            fillText.setText(parsedExampleDataSet.toString());
           
		    } catch (Exception e) {
		            /* Display any Error to the GUI. */
		    		fillText.setText("Error: " + e.getMessage());
		            Log.e(MY_DEBUG_TAG, "myQueryError", e);
		    }
    }
}

I have two more classes that handles the xml data, but I believe the error occurs in the main class. It's not a great problem as I'm able to restart the app and then refresh the data, but it's quite annoying and doesn't look good when it force closes.

Any help appreciated!
 
Hard to say for sure but I can offer some troubleshooting tips:

1) make sure you disconnect and close sockets when done with them, this can cause huge problems.

2) make sure that your code isn't recalled twice in a row (cause problems similar to #1)

3) make sure your XML is valid UTF-8 xml and there are no funny characters in it.

4) check the server response codes.


Also it would help to know what is at this line:

com.funrockmedia.stat.main.refreshData(main.java:68)
 
Back
Top Bottom