【问题标题】:How to retrieve values using Ksoap2 webservice如何使用 Ksoap2 网络服务检索值
【发布时间】:2014-01-13 21:01:11
【问题描述】:

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

我想按如下方式检索值数组:

Array ( [resultStatus] => true [responseObjSize] => 12 [responseObjects] => Array ( [0] => Array ( [item] => Array ( [0] => 1 [1] => 1 ) ) [1] => 数组 ( [item] => 数组 ( [0] => 2 [1] => 0 ) ) [2] => 数组 ( [item] => 数组 ( [0] => 3 [1] => 0 ) ) [3] => 数组 ( [item] => 数组 ( [0] => 4 [1] => 0 ) ) [4] => 数组 ( [item] => 数组 ( [0] => 5 [1] => 0 ) ) [5] => 数组 ( [item] => 数组 ( [0] => 6 [1] => 0 ) ) [6] => 数组 ( [ item] => 数组 ( [0] => 7 [1] => 0 ) ) [7] => 数组 ( [item] => 数组 ( [0] => 8 [1] => 0 ) ) [8 ] => 数组 ( [item] => 数组 ( [0] => 9 [1] => 0 ) ) [9] => 数组 ( [item] => 数组 ( [0] => 10 [1] = > 0 ) ) [10] => 数组 ( [item] => 数组 ( [0] => 11 [1] => 0 ) ) [11] => 数组 ( [item] => 数组 ( [0] = > 12 [1] => 0 ) ) ) )

谁能帮帮我。

【问题讨论】:

  • 我检索到如下代码的值,但无法正确检索
  • 据我所知,SOAP 依赖于 XML 信息集,您上面显示的结构类似于 JSON 结构。你能在这里分享你的部分 XML 响应吗?

标签: android web-services android-ksoap2


【解决方案1】:

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 我在 ksoap2 中遇到问题。它正在响应身份验证失败您能帮我解决这个问题吗?
【解决方案2】:

dipali 的代码可以帮助您使用 webService。 获得 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-08-18
    相关资源
    最近更新 更多