0

我正在使用 XML sax 解析来通过 Web 服务获取数据。它在 2.2 和 2.3 中运行良好,但在 4.x(ICS) 等更高版本中却不行。这里我的代码是...

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());
            }
                 }


        });

这是我的处理程序类代码...

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();
            }



}

这是我的日志猫。

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)

在 Android 4.x 版本中,SAX 解析不会从 Web 服务读取/获取数据。我应该在这里改变什么才能在 4.0 中工作。请帮我。

4

1 回答 1

3

你确定它没有抛出一个NetworkOnMainThreadException?看起来您的代码onClickListener在您的 mainActivity中,在这种情况下,您在那里创建的处理程序附加到您的主线程。因此,当您创建一个可运行对象并将其传递给postDelayed它时,它将在主线程上运行。由于您不能再在 4.0 版的主线程上执行网络操作,这将抛出一个NetworkOnMainThreadException,而相同的代码在 2.x 上可以正常工作。

于 2012-10-25T15:53:33.030 回答