Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:00:22+00:00 2026-05-31T10:00:22+00:00

I want to parsing Xml from server, but it take long time and finaly

  • 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. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:00:24+00:00Added an answer on May 31, 2026 at 10:00 am

    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 .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi at all I want to parsing a date from XML with this format:
I am parsing the public timeline xml from twitter and want to display images
I have this NSXMLParser that parsing an XML file from a webserver parser =
I have a flex code that i got from a friend. But i want
i am working on xml parsing and getting three item numbers from the server
today I want to try parsing an xml data from an URL. I use
I want to parse xml webservice in my phonegap application but i don't have
I am parsing an XML from a URL and showing it. I want to
I have problems parsing response from SOAP sever with jQuery. I want to convert
Basically I want to write a class for parsing XML document for my iphone

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.