gpt4 book ai didi

android - 无法从 Android 客户端调用 C# .net Web 服务方法

转载 作者:数据小太阳 更新时间:2023-10-29 02:37:24 25 4
gpt4 key购买 nike

我正在尝试使用 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: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope (position:START_TAG @2:44 in java.io.InputStreamReader@43e593c8)

请帮忙..

最佳答案

这是工作代码

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());
}
}

关于android - 无法从 Android 客户端调用 C# .net Web 服务方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7054830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com