Question

I am parsing online Xml using XmlPullParserFactory and showing the details in a listview. I have put the condition for checking internet, but sometimes it takes time to fetch data and crashes.The code which I am using is below.

      private void runningABackProcess() {
      Handler mHandler = new Handler(Looper.getMainLooper());
      Runnable myRunnable = new Runnable() {                        
      public void run() {
       try{
             //Thread.sleep(5000);

         new DoSomeTask().execute();                           

                  }catch(Exception e){}
                           // myPd_ring.dismiss();
              }
           };
           mHandler.post(myRunnable);

    }
         private class DoSomeTask extends AsyncTask<Void, Void, Void>
     {


        @Override
         protected Void doInBackground(Void... arg0) {
                        try { 
         isInternetPresent = icd.isConnectingToInternet
         if(isInternetPresent){                             
              URL url = new     URL("njhgjghjkjhkjl");                                 
              XmlPullParserFactory factory=XmlPullParserFactory.newInstance();  
              XmlPullParser xpp=factory.newPullParser();      
              xpp.setInput(getInputStream(url), "UTF_8");                                   
              int eventType=xpp.getEventType(); 
              if(eventType!=XmlPullParser.END_DOCUMENT)
                  {
            if(eventType==XmlPullParser.START_DOCUMENT)     
                 {       
                 while(eventType!=XmlPullParser.END_DOCUMENT )              
                  {              
                              xpp_name=xpp.getName();                                   
                      if((xpp_name!=null) ) 
                                {
                                   --------------------
                                   -------------------------
                                }
                            }
                    }
               }

and

      public InputStream getInputStream(URL url) {
  try {  
       InputStream str=null;
       isInternetPresent=icd.isConnectingToInternet();
      if(isInternetPresent)
           {    
         str=url.openConnection().getInputStream(); 
         if(str!= null)
             {
             return str; 
          }else{
         new AlertDialogBox(getActivity()).show(getFragmentManager(),"MyDialog");
          }
    }else{
    new AlertDialogBox(getActivity()).show(getFragmentManager(), "MyDialog");
                            }
                                return    str;              
                            //}
                        } catch (IOException e) {

                            return null;
                          }

                     }

Below is the erroe I am getting: enter image description here

Was it helpful?

Solution 2

I solved the problem by changing

 xpp.setInput(getInputStream(url), "UTF_8"); to 
 xpp.setInput(url.openConnection().getInputStream(), "UTF_8");

OTHER TIPS

This seems to be the NetworkOnMainThreadException. In that case to resolve it use AsyncTask to run code that accesses Network.

It causes because of accessing Network or Network related tasks on the main UI thread. This might sometimes hang or crash your app; freezes your app for some time. That is why you are recommended to use AsyncTask to resolve this issue.

How to use AysncTask, read from here it is very easy:

http://developer.android.com/reference/android/os/AsyncTask.html

Or you can add this line in your code that will allow Network accesses on main thread:

// Allow network thread to avoid NetworkOnMainThreadException.

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
        .permitAll().build();
StrictMode.setThreadPolicy(policy);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top