我正在尝试运行一个使用 Web 服务的 Android 应用程序的简单示例。按照网上的一些例子,我终于做到了:
private String getValueFromWS()
{
try
{
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
String weight = "3700";
String fromUnit = "Grams";
String toUnit = "Kilograms";
PropertyInfo weightProp =new PropertyInfo();
weightProp.setName("Weight");
weightProp.setValue(weight);
weightProp.setType(double.class);
request.addProperty(weightProp);
PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("FromUnit");
fromProp.setValue(fromUnit);
fromProp.setType(String.class);
request.addProperty(fromProp);
PropertyInfo toProp =new PropertyInfo();
toProp.setName("ToUnit");
toProp.setValue(toUnit);
toProp.setType(String.class);
request.addProperty(toProp);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
return response.toString();
}
catch (Exception e)
{
return "Error: " + e.getMessage();
}
}
我还添加了
<uses-permission android:name="android.permission.INTERNET"/>
在清单文件中。
我不知道为什么它会抛出异常
androidHttpTransport.call(SOAP_ACTION, envelope);
当它与模拟器一起运行时。我使用的是 2.5.8 版本的 kSoap,模拟器运行的是 Android 4.1(16 级)。
我做错了什么?