org.xmlpull.v1.XmlPullParserException:连接到soap webservice

时间:2016-10-19 10:23:08

标签: android soap ksoap2

我正在使用ksoap2库进行网络服务调用:

我收到了错误:

 androidHttpTransport.call(action, envelope);

完整代码:

public String executeSoapWebservice(String urlString, String action, String method, String nameSpace, JSONObject postParams){

    String response ="";

    SoapObject request = new SoapObject(nameSpace, method);

    try{
        JSONObject ob = postParams;
        Iterator iterator = ob.keys();
        while(iterator.hasNext()){
            String key = (String)iterator.next();
            String value = ob.getString(key);

            request.addProperty(key, value);
        }
    }catch (JSONException e) {
        e.printStackTrace();
    }

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

    try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(urlString);
        //this is the actual part that will call the webservice
        androidHttpTransport.call(action, envelope);
        // Get the SoapResult from the envelope body.

        //Object result = (Object) envelope.getResponse();
        SoapObject result = (SoapObject)envelope.bodyIn;
        if(result != null) {
            //Get the first property and change the label text
            response = result.getProperty(0).toString();
            //response = result.toString();
        }

    } catch (Exception e) {
        response = "";
        e.printStackTrace();
    }
    return response;
}

1 个答案:

答案 0 :(得分:0)

如果您可以访问Web服务(我假设它是asp.net),那么我能够通过此问题的唯一方法是进行两次修改。

在asp.net上:

[WebMethod]
public string GetJSonData()
{
   //service code

    var serializer = new JavaScriptSerializer();
    var serializedResult = serializer.Serialize(passwordData);

    String jsonString = serializedResult.ToString();

    // remove this line
    //Context.Response.Write(serializedResult);

    // return the serialized data as string
    return jsonString;
}

然后在Android方面将SoapObject更改为SoapPrimitive

// This causes the problem
//SoapObject resultData = (SoapObject) envelope.getResponse();

// Combined with the modification on the asp.net this works
SoapPrimitive resultData = (SoapPrimitive) envelope.getResponse();