Android和SOAP Web服务

时间:2012-03-13 16:54:46

标签: android soap wsdl ksoap2 android-ksoap2

我正在尝试将我的Android应用程序与我的SOAP Web服务接口,我得到了意想不到的结果......

Soap服务工作正常,它们已经过SoapUI测试,WSDL是有效的。 我正在使用kso​​ap2 lib版本1.6.1

这是我的主叫代码:

    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        request.addProperty("prop1", 1);
        request.addProperty("prop2", "FR");
        envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.setOutputSoapObject(request);
        androidHttpTransport = new HttpTransportSE(URL);
        androidHttpTransport.debug = true;
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject objetSOAP = (SoapObject)envelope.getResponse();
        Log.e("TESTSOAP", objetSOAP.getProperty(0).toString());
    }
    catch (Exception e){
        Log.i("androidHttpTransport.requestDump", androidHttpTransport.requestDump);
        e.printStackTrace();
        Log.i("androidHttpTransport.responseDump", androidHttpTransport.responseDump);


    }

我在“通话”方法中遇到了一个例外,即:

03-13 17:31:05.791: W/System.err(6020):     at 03-13 17:30:19.901: W/System.err(5976):            org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG .... @2:402 in java.io.InputStreamReader@46294968) 

奇怪的是,如果我使用requestDump来获取SoapUI中发送的信封来试用它,它的工作原理非常好。我得到了预期的结果。但是responseDump实际上是将wsdl文件返回给我,而不是结果。就像wsdl被称为正常,但不是我试图调用的方法。

任何人都知道为什么?也许如何纠正它?

谢谢!

2 个答案:

答案 0 :(得分:0)

该死的,终于找到了解决方案(几个月后!) 显然,

   androidHttpTransport = new HttpTransportSE(URL);

应该指向wsdl,而不是指向php文件。

答案 1 :(得分:0)

类soapDemo扩展了AsyncTask         {

        private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        private final String SOAP_ACTION = "YOUR_PATH";
        private final String METHOD_NAME = "METHOD_NAME";
        private final String NAMESPACE = "NAMESPACE (for ex.http://)";
        private final String URL = "URL";

    @Override
    protected void onPreExecute() 
    {
        this.dialog.setMessage("data Loading");
        this.dialog.show();
    }

    @Override
    protected Void doInBackground(Void... unused) 
    {

        SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
        Request.addProperty("KEY",  "VALUE");
        Request.addProperty("KEY",  "VALUE");

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = true;
        envelope.setOutputSoapObject(Request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try 
    {
        androidHttpTransport.call(SOAP_ACTION, envelope);
        SoapObject response = (SoapObject) envelope.getResponse();

        System.out.println("response"+response);
        int  Count = response.getPropertyCount();
        list= new String[Count];    


        for (int i = 0; i < Count; i++)
        {
           list[i] = response.getPropertyAsString(i).toString();
        }

    }

    catch (Exception e) 
    {
    exc=true;
    e.printStackTrace();
    }
    return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {

        if (this.dialog.isShowing()) 
        {
           this.dialog.dismiss();
        }
        if(exc)
        {
           Toast.makeText(MainActivity.this,"Error" , Toast.LENGTH_LONG).show();
        }
        else
        {
            spinner();
            exc=false;
        }
     }
    }

    public void spinner()
    {
        Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner1.setAdapter(adapter1);

        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() 
        {
            @Override
            public void onNothingSelected(AdapterView<?> parent)
            {

            }

            @Override
            public void onItemSelected(AdapterView<?> parent,View view, int pos, long id) 
            {
            // your code
            }

      });
    }