在Android中调用SOAP webservices时出现org.xmlpull.v1.XmlPullParserException

时间:2014-08-02 06:45:36

标签: android android-ksoap2 android-webservice

在我的应用程序中,我在该登录页面中创建了登录页面我输入了用户名和密码详细信息,并且当我尝试单击登录按钮时,我发送了请求参数,并获取用户名和密码作为响应从给定的SOAP URL,但当我尝试获得一个respose xml pull解析器异常将occer我在这里犯了错误我附加了我的代码将任何人帮我从这个异常。

LoginActivity

 public class LoginActivity extends Activity {
        //Set Error Status
        static boolean errored = false;
        Button Login;
        TextView Showtext;
        EditText userName , passWord;   
        ProgressBar webservicePG;
        String editTextUsername;
        boolean loginStatus;
        String editTextPassword;
        public static   String nameValue;
        public static   String passwordvalue;

        int MC_loginRequest = 0;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
             SharedPreferences prefs = this.getSharedPreferences("com.hubino.sozo", Context.MODE_PRIVATE);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy); 
            userName = (EditText) findViewById(R.id.usernametext);
            passWord = (EditText) findViewById(R.id.passwordtext);
            Showtext = (TextView) findViewById(R.id.showtext);
            Login = (Button) findViewById(R.id.login);
            webservicePG = (ProgressBar) findViewById(R.id.progressBar1);
            Login.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    userName.setHintTextColor(getResources().getColor(R.color.red));
                    userName.setHint(Html.fromHtml("<small><small>" + 
                             getString(R.string.hint) + "</small></small>"));
                    passWord.setHintTextColor(getResources().getColor(R.color.red));
                    passWord.setHint(Html.fromHtml("<small><small>" + 
                             getString(R.string.password) + "</small></small>"));

                    if (userName.getText().length() != 0 && userName.getText().toString() != "") {
                        if(passWord.getText().length() != 0 && passWord.getText().toString() != ""){
                            editTextUsername = userName.getText().toString();
                            editTextPassword = passWord.getText().toString();
                            Showtext.setText("");
                            nameValue = userName.getText().toString();
                            passwordvalue = passWord.getText().toString();
                    haredPreferences.Editor prefs = LoginActivity.this.getSharedPreferences(
                                    "com.hubino.sozo", Context.MODE_PRIVATE).edit();

                        prefs.putString("name",nameValue);  
                        prefs.putString("number",passwordvalue);    
                        prefs.commit();
                        AsyncCallWS task = new AsyncCallWS();
                        task.execute();
                        }

                    } 
                }

          });

          }

           private class AsyncCallWS extends AsyncTask<String, Void, String> {
            @Override
            protected String doInBackground(String... params) {

                loginStatus = WebService.Loginvalue(editTextUsername,editTextPassword,"mc_login");
                loginStatus = true;
                return null;
            }

            @Override
            protected void onPostExecute(String result) {
                webservicePG.setVisibility(View.INVISIBLE);
                Intent intObj = new Intent(LoginActivity.this,MenuActivity.class);
                if(!errored){
                        if(loginStatus){
                        startActivity(intObj);
                    }else{

                        Showtext.setText("Login Failed, try again");
                    }

                }else{
                    Showtext.setText("Error occured in invoking webservice");
                }

                errored = false;
                    }

            @Override
            protected void onPreExecute() {
                webservicePG.setVisibility(View.VISIBLE);
            }

            @Override
            protected void onProgressUpdate(Void... values) {
            }
        }
        public static Object password() {
            // TODO Auto-generated method stub
            return null;
        }
    }



    **WebService**

        public class WebService extends AsyncTask<String, Void, String> {
        private static mc_loginRequest MC_loginRequest = null;
        private static final String NAMESPACE = "    ";
        private static final String URL = "    ";
        private static final String SOAP_ACTION = "  ";
        private static final String METHOD = "mc_login";

        @SuppressWarnings("null")
        public static boolean Loginvalue(String usrname,String passwd, String mc_login) {
            MC_loginRequest = new mc_loginRequest();
            boolean loginStatus = false;
            SoapObject request = new SoapObject(NAMESPACE, METHOD);
            PropertyInfo pi = new PropertyInfo();
            MC_loginRequest.username=usrname;
            MC_loginRequest.password=passwd;
            pi.setName("MC_loginRequest");
            pi.setValue(MC_loginRequest);
            pi.setType(MC_loginRequest.getClass());
            request.addProperty(pi);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.addMapping(NAMESPACE, "MC_loginRequest", mc_loginRequest.class);
            envelope.bodyOut = request;
            HttpTransportSE transport = new HttpTransportSE(URL);
            transport.debug = true;
            envelope.dotNet = true;
            envelope.encodingStyle = SoapEnvelope.ENC;
            try {
                transport.call(SOAP_ACTION+METHOD, envelope);
                SoapPrimitive loginresponse = (SoapPrimitive) envelope.getResponse();
                loginStatus = Boolean.parseBoolean(loginresponse.toString());
                }
            catch (Exception e) 
            {
                LoginActivity.errored = false;
            e.printStackTrace();
                Log.e("YOUR_APP_LOG_TAG", "I got an error", e);
            } 
            return loginStatus;
            }
        protected SoapObject doInBackground(Integer... branchNumber) {
            return null;
        }
        @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
            return null;
        }
    }


    **mc_loginRequest**

          public class mc_loginRequest implements KvmSerializable {

        public  String username;
        public  String password;
        @Override
        public Object getProperty(int arg0) {
            switch (arg0){
            case 0:
                return username;
            case 1:
                return password;

             default:
                 return null;
            }
        }

        @Override
        public int getPropertyCount() {
            // TODO Auto-generated method stub
            return 1;
        }
        @Override
        public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
            switch(index)
            {
            case 0:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "username";
                break;
            case 1:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "password";
                break;

            default:
                break;
            }
    }
        @Override
        public void setProperty(int index, Object value) {
            switch(index)
            {
            case 0:
                username = value.toString();
                break;
            case 1:
                password = value.toString();
                break;
             default:
            break;
           }
        }
    }

`

1 个答案:

答案 0 :(得分:1)

当你的文件从请求中获得损坏时,你会得到“XML pull parser exception”,可能是错误的封闭标签或类似“HTTP error 503”的结果,当服务器不可用时会出现这种异常。

我建议您在SOAPObject调用

中为不同类型的异常提供额外的catch关闭

P.S:提供你得到的回应