0

I want to parsing Xml from server, but it take long time and finaly throws NetworkOnMainThreadException. I have using strict mode, but it still thows NetworkOnMainThreadException.

Update : I try run aplication on emulator,this exception not show. this exception show when i run aplication on real device.

this my code to parsing xml :

public class ParsingXML extends Thread{
            //private Context ctx;
    private int status;
    private String raw_url;
    Context mctx;
    public DefaultHandler getMyExampleHandler() {
        return myExampleHandler;
    }
    DefaultHandler myExampleHandler=null;

    public ParsingXML(Context ctx , int status){
            //this.ctx=ctx;
            this.status=status;
            this.mctx=ctx;
            if(status==constant.GET_LIST_PRODUCT){
                raw_url=constant.URL+"listBarang.php";
            }
    }
    public ParsingXML(Context ctx,int status,String id){
        this.status=status;
        this.mctx=ctx;
        if(status==constant.GET_DETAIL_PRODUCT){
            raw_url=constant.URL+"detailBarang.php?id="+id;
        }
    }
    public void parse(){    
        try {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
                URL url = new URL(raw_url);

                SAXParserFactory spf = SAXParserFactory.newInstance();
                SAXParser sp = spf.newSAXParser();
                XMLReader xr = sp.getXMLReader();

                if(status==constant.GET_LIST_PRODUCT){
                    myExampleHandler = new ListProductHandler();
                }
                else if(status==constant.GET_DETAIL_PRODUCT){
                    myExampleHandler = new DetailProductHandler();
                }
                xr.setContentHandler(myExampleHandler);

                xr.parse(new InputSource(url.openStream()));

        } catch (Exception e) {
            Toast.makeText(mctx, "Connection Error", Toast.LENGTH_LONG).show();
        }
                /* Display the TextView. */
    }
}

someone can help me please?? any help very apreciated. :)

ZeeDroid
  • 145
  • 1
  • 11

2 Answers2

2

Include your network connection and parsing data to and from different server activities in a separate background thread. There is a class call AsyncTask to do this. Include your XML reading code snippets in the doInBackground() method. After reading all XML file you can show the result in onPostExecute() method.

Using the Thread class is deprecated. This is the link to documentation for more details. This process will get rid you from the NetworkOnMainThreadException .

AnujAroshA
  • 4,623
  • 8
  • 56
  • 99
  • Your update saw later. I guess you are running your code in a device that is higher than platform 3. It will give you `NetworkOnMainThreadException` when you are not using background thread for Internet connectivity. – AnujAroshA Mar 16 '12 at 09:22
  • I meant don't use Thread class, instead use `AsyncTask` class. And do all your background tasks like parsing XML in the `doInBackground` method. – AnujAroshA Mar 16 '12 at 11:01
0

although you extend the thread, but your method parse also dont run in thread way, please put the parse method in run method, for example:

public void run(){
    parse();
}
idiottiger
  • 5,147
  • 2
  • 25
  • 21
  • But it throws new Exception :An error occured while executing doInBackground(). is it posible when start that thread on Asynctask???because i start parsing thread on AsyncTask. – ZeeDroid Mar 16 '12 at 09:09
  • and also this exception : WindowManager(3494): Activity com.portalshop.zendy.ListProductLama has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40821138 that was originally added here – ZeeDroid Mar 16 '12 at 09:12
  • `doInBackground` mean another thread not the ui thread, your exception maybe like this: http://stackoverflow.com/questions/2850573/activity-has-leaked-window-that-was-originally-added – idiottiger Mar 16 '12 at 09:17