【问题标题】:Can not call C# .net Web Service method from Android client无法从 Android 客户端调用 C# .net Web Service 方法
【发布时间】:2011-08-14 03:34:09
【问题描述】:

我正在尝试使用 ksoap 库从 Android 客户端调用 Web 服务。

这是我的安卓代码

private static final String SOAP_ACTION = "http://tempuri.org/HelloWorld";
private static final String METHOD_NAME = "HelloWorld";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://192.16.0.230/WebService/Test.asmx";
TextView tv;

public void call()
{
    try {

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        request.addProperty("name", "zawoad");

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

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.call(SOAP_ACTION, envelope);

        String result = (String)envelope.getResponse();

        tv.setText(result);
    } catch (Exception e) {
        tv.setText("exception :" + e.getLocalizedMessage());
        }
}

这是我在 Test.asmx 文件中编写的 Web 服务方法

[WebMethod]
public string HelloWorld(string name)
{
    return "Hello World" + name;
}

androidHttpTransport.call(SOAP_ACTION, envelope); 行执行时会抛出以下异常

org.xmlpull.v1.XmlPullParserException:预期:START_TAG {http://schemas.xmlsoap.org/soap/envelope/}信封(位置:START_TAG @2:44 in java.io.InputStreamReader@43e593c8)

请帮忙..

【问题讨论】:

    标签: android xml web-services soap


    【解决方案1】:

    这是工作代码

    private static final String SOAP_ACTION = "http://tempuri.org";
    private static final String METHOD_NAME = "HelloWorld";
    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://192.16.0.230/WebService/Test.asmx?wsdl";
    /*write ?wsdl only for local system testing*/
    
    TextView tv;
    
    public void call()
    {
        try {
    
            SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    
            request.addProperty("name", "zawoad");
    
            SoapSerializationEnvelope envelope = new          SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(request);
    
            HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,20000);//Updated
    
            androidHttpTransport.call(SOAP_ACTION, envelope);
    SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();
            String result = resultsRequestSOAP.toString();
    
            tv.setText(result);
        } catch (Exception e) {
            tv.setText("exception :" + e.getLocalizedMessage());
            }
    }
    

    【讨论】:

      【解决方案2】:

      您正在执行的呼叫不会发生。 什么是 Web 服务返回类型?我们可以传递值并调用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多