如何使用 Ksoap2 网络服务检索值



我想从 android 中的 java webservice 检索值数组。我正在使用带有依赖项的 ksoap 3.0 jar。

我想检索值数组,如下所示:

数组 ( [结果状态] => true [响应对象大小] => 12 [响应对象] => 数组 ( [0] => 数组 ( [项目] => 数组 ( [0] => 1 [1] => 1 ) ) [1] => 数组 ( [项目] => 数组 ( [0] => 2 [1] => 0 ) ) [2] => 数组 ( [项目] => 数组 ( [0] => 3 [1] => 0 ) ) [3] => 数组 ( [项目] => 数组 ( [0] => 4 [1] => 0 ) ) [4] => 数组 ( [项目] => 数组 ( [0] => 5 [1] => 0 ) ) [5] => 数组 ( [项目] => 数组 ( [0] => 6 [1] => 0 ) ) [6] => 数组 ( [项目] => 数组 ( [0] => 7 [1] => 0 ) ) [7] =>

数组 ( [项目] =>数组 ( [0

] => 8 [1] => 0 ) ) [8] => 数组 ( [项目] => 数组 ( [0] => 9 [1] => 0 ) ) [9] => 数组 ( [项目] => 数组 ( [0] => 10 [1] => 0 ) ) [10] => 数组 ( [项目] => 数组 ( [0] => 11 [1] => 0 ) ) [11] => 数组 ( [项目] => 数组 ( [0] => 12 [1] => 0 ) ) )

谁能帮我。

public static final String NAMESPACE = "http://tempuri.org/";

 public static String GetColor(Context c) throws IOException,
            XmlPullParserException {
        String METHOD_NAME = "GetColor";
        String SOAP_ACTION = "http://tempuri.org/youruri name/";
        SOAP_ACTION = SOAP_ACTION + METHOD_NAME;
        SoapObject request = new SoapObject(NAMESPACE,
                METHOD_NAME);
        // Use this to add parameters
        // Declare the version of the SOAP request
        return WebCalls.call(c, request, .NAMESPACE, METHOD_NAME,
                SOAP_ACTION);
    } 



public static String call(Context c, SoapObject request, String NAMESPACE,
                String METHOD_NAME, String SOAP_ACTION) throws IOException,
                XmlPullParserException {
            Log.i(WebCalls, "URL " + your URL);
            Log.i(WebCalls, "Method Name " + METHOD_NAME);
            Log.i(WebCalls, "request Name " + request);
            String SoapResult = null;
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                    SoapEnvelope.VER11);
            envelope.setOutputSoapObject(request);
            envelope.dotNet = true;
            HttpTransportSE androidHttpTransport = new HttpTransportSE(
                    CommonVariable.URL);
            // this is the actual part that will call the webservice
            androidHttpTransport.call(SOAP_ACTION, envelope);
            // Get the SoapResult from the envelope body.
            if (envelope.bodyIn instanceof SoapFault) {
                SoapResult = ((SoapFault) envelope.bodyIn).faultstring;
            } else {
                SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
                SoapResult = resultsRequestSOAP.getProperty(0).toString();
            }
            Log.i(WebCalls, "Response " + SoapResult);
            return SoapResult;
        }

我希望这也是有用的方法,你...如果有任何疑问,请告诉我..这段代码已经在我当前的项目中实现,现在它工作正常。

dipali的代码可以帮助您使用Web服务。获得 SoapResult 后,您必须对其进行解组才能获取所有响应元素。

如果您的 Soap 响应只是一个整数数组,下面是一个示例:

SoapObject obj =(SoapObject) resultsRequestSOAP.getProperty(0);
for(int i=0; i<obj.getPropertyCount(); i++)
{
   int id= Integer.parseInt(obj.getProperty(i).toString());
   //Do what you want
}

最新更新