0

我编写了一个使用 SOAP 函数从 Web 服务获取的代码,问题是响应不是 xml 字符串,我需要知道的是,来自我的 android 代码 SOAP 函数的错误?还是来自网络服务?为什么结果是这样

 public String SOAP_ACTION2 = "http://tempuri.org/GetDataTable";

    public String OPERATION_NAME2 = "GetDataTable";

    public String SOAP_ADDRESS2 = "http://192.168.0.15/EServicedesk/DesktopModules/EServiceDesk.Website/ESDWebService/Auth.asmx?WSDL";
Object response;
String XMLData = null;
    SoapSerializationEnvelope envelope;
    SoapRequestTask2 task;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        task = new SoapRequestTask2();
        try { 
            XMLData = task.execute().get();
            Log.i("task ", XMLData);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

}

private class SoapRequestTask2 extends AsyncTask<Void, Void, String> {
        // runs on ui thread.
        private String data;

        protected void onPreExecute() {

        }

        // runs in the background thread. do not update ui from here
        protected String doInBackground(Void... params) {
            SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
                    OPERATION_NAME2);
            PropertyInfo pi = new PropertyInfo();
            pi.setName("UserID");
            pi.setValue(193);
            pi.setType(Integer.class);
            request.addProperty(pi);


            envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;

            envelope.setOutputSoapObject(request);

            HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS2);

            response = null;
            try {
                // httpTransport.debug=true;
                httpTransport.call(SOAP_ACTION2, envelope);
                // String ss = httpTransport.requestDump;
                response = envelope.getResponse();

                data = response.toString();
            //  Log.i("respons", envelope.getResponse().toString());
            } catch (Exception exception) {
                response = exception.toString();
                XMLData = response.toString();
            }

            return data;
        }

结果如下:任务 2:anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{}; 元素=anyType{}; }; }; }; }; }; }; }; diffgram=anyType{DocumentElement=anyType{TargetTable=anyType{RequestHotList=MyRequests; 计数=927;}; TargetTable=anyType{RequestHotList=MyAssignedRequests; 计数=603;}; TargetTable=anyType{RequestHotList=MyServiceDeskRequests; 计数=969;}; TargetTable=anyType{RequestHotList=MyWorkGroupRequests; 计数=770;}; TargetTable=anyType{RequestHotList=MyApproval; 计数=82;}; }; }; }

4

1 回答 1

0

你可以做这样的事情

Web 服务返回对象数组

您可以通过转到特定节点来获取每个对象的每个值的值,然后使用 getProperty() 方法获取所需对象的值,如下所示。//旧的网络服务。

public void  getLocation()
{
    OPERATION_NAME = "GetLd";   
    SOAP_ACTION = Constants.strNAMESPACE+""+OPERATION_NAME;     
        REQUST_ID=3;
        final Responce responce=new Responce();
        Thread thread=new Thread(new Runnable() 
        {

            SoapObject response;                
            @Override
            public void run() 
            {
                try
                {
                SoapObject request=new SoapObject(Constants.strNAMESPACE, OPERATION_NAME);
                SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
                envelope.setOutputSoapObject(request);
                HttpTransportSE ht = new HttpTransportSE(Constants.strWEB_SERVICE_URL);
                ht.call(SOAP_ACTION, envelope);
                response = (SoapObject)envelope.bodyIn;
                if(response!=null)
                {
                     SoapObject str = null;
                        for(int i=0;i<response.getPropertyCount();i++)
                                str=(SoapObject) response.getProperty(i);       


                        SoapObject str1 = (SoapObject) str.getProperty(0);

                        SoapObject str2 = null;
                        ArrayList<Location> arrData=new ArrayList<Location>();
                        for(int j=0;j<str1.getPropertyCount();j++)
                        {                       
                            str2 = (SoapObject) str1.getProperty(j);
                            Location location=new Location();
                            if(str2!=null)
                            {   
                                responce.Result_Type=Constants.DataPresent;

                                if(isValidProperty(str2, "LocationName"))
                                {
                                    location.setName(str2.getProperty("LocationName").toString());
                                }
                                if(isValidProperty(str2, "LocationId"))
                                {
                                    location.setId(Integer.parseInt(str2.getProperty("LocationId").toString()));
                                }   
                                arrData.add(location);
                            }
                            else
                            {
                                responce.Result_Type=Constants.NoDataPresent;
                            }                               
                        }
                        responce.ResponceData=arrData;
                        Log.d("Location", "------------------------");
                        for(int i=0;i<arrData.size();i++)
                            Log.d("Tag", arrData.get(i).toString());
                      }
                }          
                catch (Exception e) 
                {       
                    e.printStackTrace();
                    responce.Result_Type=Constants.Error;
                }
                finally
                {
                    Message message=new Message();
                    message.what=REQUST_ID;
                    message.arg1=responce.Result_Type;
                    message.obj=responce;               
                    handler.sendMessage(message);
                }       
            }

        });
        thread.start();
}







 boolean isValidProperty(SoapObject soapObject,String PropertyName)
    {
        if(soapObject!=null)
        {
            if(soapObject.hasProperty(PropertyName))
            {
                if(!soapObject.getProperty(PropertyName).toString().equalsIgnoreCase("")&&!soapObject.getProperty(PropertyName).toString().equalsIgnoreCase("anyType{}"))
                    return true;
            else
                return false;
            }
            return false;

        }
        else
            return false;

    }
于 2013-08-04T07:08:54.597 回答