XmlPullParserException(调用webservice时的Android)

时间:2014-04-02 07:47:09

标签: android web-services

当我想用httpTransport调用webservice时,它会显示这个错误。(我的web服务是用microsoft c#web应用程序创建的)

    package com.example.mobile_e_commerce;

    import java.util.ResourceBundle;

    import org.ksoap2.SoapEnvelope;
    import org.ksoap2.serialization.SoapObject;
    import org.ksoap2.serialization.SoapPrimitive;
    import org.ksoap2.serialization.SoapSerializationEnvelope;
    import org.ksoap2.transport.HttpTransportSE;

    import android.app.Activity;
    import android.app.Fragment;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    public class ProductCollection extends Activity {

        private LayoutInflater inflater;
        private ViewGroup container;

        private static final ResourceBundle rb = ResourceBundle.getBundle("com.example.mobile_e_commerce.webserviceurl");

//My Soapinformation for calling the webservice later//

        private final String NAMESPACE = rb.getString("WSDLTargetNamespace");
      private  final String SoapURL = rb.getString("SoapAddress");
       private final String SOAP_ACTION = rb.getString("SoapAction");
        private final String METHOD_NAME = rb.getString("OperationName");


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_product_collection);

            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
        }

        @Override
        protected void onResume(){
            super.onResume();
            try{

                    (new dataLoader()).execute();
            }
            catch(Exception ee)
            {
                ee.printStackTrace();
                Log.e("Apps onResume Error", ee.toString());

                 TextView textView = (TextView)findViewById(R.id.Product1);
                 textView.setText(ee.toString());
            }
        }

        class dataLoader extends AsyncTask<String,String,String>{



                //ProductCollection pc = new ProductCollection();
                //TextView textView = new TextView(pc);
                 private String valuePass;
             @Override
              protected String doInBackground(String... arg0) {

                    SoapObject request = new SoapObject(NAMESPACE,METHOD_NAME);



                    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(

                    SoapEnvelope.VER11);

                    envelope.dotNet = true;

                    envelope.setOutputSoapObject(request);

                    HttpTransportSE httpTransport = new HttpTransportSE(SoapURL);
                    SoapPrimitive response = null;           


The error should be start here//It just somehow read html file instead of xml file so output the error.

                            try
                            {                           

                            httpTransport.call(SOAP_ACTION, envelope);
                            response = (SoapPrimitive)envelope.getResponse();
                            valuePass = response.toString();//response.toString();//trying to get from my
                                                      //webservices but url not found,solve it later
                            }
                            catch (Exception exception)
                            {
                            exception.printStackTrace();
                                Log.e("Apps Error", exception.toString());
                            }
                            return valuePass;
             }

             @Override
             protected void onPostExecute(String tV) {

                 try{
                TextView textView = (TextView)findViewById(R.id.Product1);
                  textView.setText(tV.toString());
                 }
                 catch(Exception ee)
                 {
                     Log.e("App Error: ", ee.toString());
                 }
             }
        }



        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.product_collection, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public static class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(
                        R.layout.fragment_product_collection, container, false);
                return rootView;
            }
        }

    }

来自logcat的日志

(ignore the java.lang.nullpointerexception first[the end of the line])

04-02 15:46:22.160: W/System.err(23581): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <HTML>@2:7 in java.io.InputStreamReader@b370a9e8) 
04-02 15:46:22.160: W/System.err(23581):    at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)
04-02 15:46:22.170: W/System.err(23581):    at org.ksoap2.SoapEnvelope.parse(SoapEnvelope.java:127)
04-02 15:46:22.170: W/System.err(23581):    at org.ksoap2.transport.Transport.parseResponse(Transport.java:96)
04-02 15:46:22.170: W/System.err(23581):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:189)
04-02 15:46:22.170: W/System.err(23581):    at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95)
04-02 15:46:22.170: W/System.err(23581):    at com.example.mobile_e_commerce.ProductCollection$dataLoader.doInBackground(ProductCollection.java:93)
04-02 15:46:22.170: W/System.err(23581):    at com.example.mobile_e_commerce.ProductCollection$dataLoader.doInBackground(ProductCollection.java:1)
04-02 15:46:22.170: W/System.err(23581):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
04-02 15:46:22.170: W/System.err(23581):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-02 15:46:22.170: W/System.err(23581):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-02 15:46:22.170: W/System.err(23581):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
04-02 15:46:22.170: W/System.err(23581):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-02 15:46:22.170: W/System.err(23581):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-02 15:46:22.170: W/System.err(23581):    at java.lang.Thread.run(Thread.java:856)
04-02 15:46:22.170: E/Apps Error(23581): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG <HTML>@2:7 in java.io.InputStreamReader@b370a9e8) 
04-02 15:46:22.260: E/App Error:(23581): java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,我想这个问题是因为我们用来连接网站的URL。所以我通过更改我的网站网址解决了这个问题,就像这个“http://www.example.com/index.php/api/v2_soap/?wsdl/”。

示例:

HttpTransportSE androidHttpTransport = new HttpTransportSE(http://www.example.com/index.php/api/v2_soap/?wsdl/,900000000); androidHttpTransport.call(“”,env);