Search code examples
androidandroid-asynctasksaxparserandroid-4.0-ice-cream-sandwichui-thread

XML SAX parsing not working in android 4.0 but in 2.x


I am using XML sax parsing for getting data through web-services. It working fine in 2.2 and 2.3 but not in higher versions like 4.x(ICS). Here my code is...

btnlogin.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                checkNetworkStatus();
                try
                {
                if(edtMailId.getText().toString().trim().equals(""))
                {
                    showDialog(1);
                }
                else if(edtPwd.getText().toString().trim().equals(""))
                {
                    showDialog(2);
                }
                else
                {
            final ProgressDialog progressdialog=ProgressDialog.show(Signin.this, "abc....", "Signing In...",true,true);
            Handler handler=new Handler();
                    Runnable r=new Runnable() {

                        public void run() {
                            try
                            {



                                String _EmailID =edtMailId.getText().toString();


                                String _Password=edtPwd.getText().toString();

                                if(chkRem.isChecked())
                                {
                                getSharedPreferences("UserData", MODE_PRIVATE).edit().putString("remUsername",edtMailId.getText().toString()).commit();
                                }
                                else
                                {
                                    getSharedPreferences("UserData", MODE_PRIVATE).edit().putString("remUsername","UnChecked").commit();
                                }
                                WebService ws=new WebService();

                                boolean loginResult=ws.checkUser(_EmailID, _Password);

                                if(loginResult){



                                    //SAXParserFactory spf=SAXParserFactory.newInstance();
                                    String URL = "http://10.0.2.2:2291/Ras.asmx/getUserDetails";
                                    String charset = "UTF-8";
                                    String query1;



                                    try {
                                    query1 = String.format("MailId=%s", URLEncoder.encode(_EmailID, charset));

                                    URLConnection url = new URL(URL + "?" + query1).openConnection();

                                    url.setRequestProperty("Accept-Charset", charset);
                                    url.setDoOutput(true);

                                    InputStream input =url.getInputStream();


                                    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
                                    SAXParser saxParser = saxParserFactory.newSAXParser();
                                    XMLReader xmlReader = saxParser.getXMLReader();



                                    //saxParser=saxParserFactory.newSAXParser();
                                    //xmlReader=saxParser.getXMLReader();
                                    MyHandler mh=new MyHandler();
                                    xmlReader.setContentHandler(mh);
                                    xmlReader.parse(new InputSource(input));
                                    Intent in=new Intent(getApplicationContext(),TabBar.class);
                                    in.setAction(Intent.ACTION_MAIN);
                                    in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(in);


                                    }

                                    catch (UnsupportedEncodingException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                    }
                                    catch (IOException e2) {
                                    // TODO Auto-generated catch block
                                    e2.printStackTrace();
                                    }
                                    catch (ParserConfigurationException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                    }
                                    catch (SAXException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                    }

                                    catch (Exception e2) {
                                    // TODO Auto-generated catch block
                                    e2.printStackTrace();
                                    }






                                    }

                                else
                                {
                                    txtErrormsg.setVisibility(View.VISIBLE);
                                    txtErrormsg.setText("Incorrect Email Address or Password");
                                }   



                            }
                            catch(Exception e)
                            {
                                //txtErrormsg.setVisibility(View.VISIBLE);
                                txtErrormsg.setText("Server Busy");
                                 Log.e("SignIn", e.getMessage());
                            }
                            progressdialog.dismiss();

                        }
                    };

                handler.postDelayed(r, 1000);
            }
            }

            catch(Exception ex)
            {
                Log.e("Server Busy",ex.getMessage().toString());
            }
                 }


        });

Here my HANDLER class code...

class  MyHandler extends DefaultHandler
        {
           boolean is_FirstName=false;
           boolean is_UserID=false;



           @Override
            public void startDocument() throws SAXException {
                // TODO Auto-generated method stub
                super.startDocument();
            }

            @Override
            public void startElement(String uri, String localName, String name,
                    Attributes attributes) throws SAXException {
                // TODO Auto-generated method stub


                super.startElement(uri, localName, name, attributes);
                if(localName.equals("FirstName")){
                    is_FirstName=true;
                }
                else if(localName.equals("UserID")){
                    is_UserID=true;
                }

        }

            @Override
            public void characters(char[] ch, int start, int length)
                    throws SAXException {
                // TODO Auto-generated method stub
                SharedPreferences.Editor edit=splogin.edit();
                super.characters(ch, start, length);
                if(is_FirstName){


                    edit.putString("FirstName", new String(ch,start,length));
                }
                else if(is_UserID){


                    edit.putString("UserID", new String(ch,start,length));
                }


                edit.commit();
            }


            @Override
            public void endElement(String uri, String localName, String name)
                    throws SAXException {
                // TODO Auto-generated method stub
                super.endElement(uri, localName, name);
                if(localName.equals("FirstName")){
                    is_FirstName=false;
                }
                else if(localName.equals("UserID")){
                    is_UserID=false;
                }

            }

            @Override
            public void endDocument() throws SAXException {
                // TODO Auto-generated method stub
                super.endDocument();
            }



}

Here my log cat.

10-25 21:52:09.090: W/System.err(546): java.io.FileNotFoundException: http://0.0.0.0/rWS/r.asmx/getUserDetails?MailId=tej%40gmail.com
10-25 21:52:09.090: W/System.err(546):  at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:177)
10-25 21:52:09.090: W/System.err(546):  at com.eist.stats.Signin$1$2$1.run(Signin.java:194)
10-25 21:52:09.090: W/System.err(546):  at android.os.Handler.handleCallback(Handler.java:605)
10-25 21:52:09.133: W/System.err(546):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 21:52:09.140: W/System.err(546):  at android.os.Looper.loop(Looper.java:137)
10-25 21:52:09.140: W/System.err(546):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-25 21:52:09.140: W/System.err(546):  at java.lang.reflect.Method.invokeNative(Native Method)
10-25 21:52:09.140: W/System.err(546):  at java.lang.reflect.Method.invoke(Method.java:511)
10-25 21:52:09.140: W/System.err(546):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-25 21:52:09.151: W/System.err(546):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-25 21:52:09.151: W/System.err(546):  at dalvik.system.NativeStart.main(Native Method)

SAX parsing is not read/get the data from Web-Service in Android 4.x versions. What should I change here to work in 4.0. Please help me.


Solution

  • Are you sure it's not throwing a NetworkOnMainThreadException? It looks like the code for your onClickListener is in your main Activity, in which case the handler that you create there is attached to your main thread. Thus, when you create a runnable and pass it into postDelayed it will be run on the main thread. Since you can no longer perform a network operation on the main thread in version 4.0 this would throw a NetworkOnMainThreadException, whereas the same code would work OK on 2.x.