XML : Run method not getting called in Thread

I am trying to parse xml using a seperate thread. The small code snippet of the entire code is as below:

  Thread t = new Thread(new Runnable() {          @Override          public void run() {             try{              c=c1;              URL url = new URL(url1);              HttpURLConnection con= (HttpURLConnection) url.openConnection();              InputStream i= con.getInputStream();              xf=XmlPullParserFactory.newInstance();              xp=xf.newPullParser();              xp.setInput(i,null);              parseXML(xp,c);          }          catch(Exception e)          {              e.printStackTrace();            }              }      });      t.start();    

But it seems the run method is not getting called itself. Can someone please help e find out what am I missing here.

EDIT: Following is the whole code related to XML:

  public void fetchXml(String u,final Context c1) throws IOException,   XmlPullParserException, InterruptedException {      Thread t = new Thread(new Runnable() {          @Override          public void run() {             try{                c=c1;                 URL url = new URL(url1);              HttpURLConnection con= (HttpURLConnection) url.openConnection();              InputStream i= con.getInputStream();              xf=XmlPullParserFactory.newInstance();              xp=xf.newPullParser();              xp.setInput(i,null);              parseXML(xp,c);          }          catch(Exception e)          {                e.printStackTrace();            }       }      });        t.start();      t.join();     }        public void parseXML(XmlPullParser xp,Context c) throws XmlPullParserException, IOException {        int event;      String text=null;      Toast.makeText(c, "inside parse", Toast.LENGTH_LONG).show();        event=xp.getEventType();        while(event!=XmlPullParser.END_DOCUMENT)      {            String name=xp.getName();            switch(event)          {                case XmlPullParser.START_TAG :                  break;                case XmlPullParser.TEXT :                  text=xp.getText();                  break;                case XmlPullParser.END_TAG :                  if(name.equals("country")){                      country=text;                      Toast.makeText(c, "Country"+country, Toast.LENGTH_LONG).show();                  }                  else if(name.equals("humidity")){                        humidity=xp.getAttributeValue(null,"value");                      Toast.makeText(c, "humidity"+humidity,  Toast.LENGTH_LONG).show();                  }                    else if (name.equals("pressure"))                  {                      pressure=xp.getAttributeValue(null,"value");                      Toast.makeText(c, "pressure"+pressure,  Toast.LENGTH_LONG).show();                  }                  else{}                  break;     }          event=xp.next();    }      parsingcomplete=true;    }    

Also when I checked the logcat I am getting the following error for the Toast statement I wrote in inside the parsexml function above.

   04-11 07:59:08.891 31361-31580/com.example.hp.xmlparsing W/System.err:   java.lang.RuntimeException: Can't create handler inside thread that has not  called Looper.prepare()   04-11 07:59:08.892 31361-31580/com.example.hp.xmlparsing W/System.err:     at android.os.Handler.<init>(Handler.java:200)    

No comments:

Post a Comment