android - 使用 KSOAP 的 Web 服务

标签 android web-services ksoap2 android-ksoap2

我正在开发一个货币转换器应用程序,但我无法获得正确的输出(我得到的是零)。

我正在使用来自 http://www.webservicex.net/ws/WSDetails.aspx?CATID=2&WSID=10 的网络服务.

WSDL 将函数声明为:

<wsdl:types>
  <s:schema elementFormDefault="qualified" targetNamespace="http://www.webserviceX.NET/">
  <s:element name="ConversionRate">
  <s:complexType>
  <s:sequence>
  <s:element minOccurs="1" maxOccurs="1" name="FromCurrency" type="tns:Currency"/>
  <s:element minOccurs="1" maxOccurs="1" name="ToCurrency" type="tns:Currency"/>
  </s:sequence>
</s:complexType>
</s:element>
<s:simpleType name="Currency">
  <s:restriction base="s:string">
   <s:enumeration value="AFA"/>
   <s:enumeration value="ALL"/>
   <s:enumeration value="DZD"/>
   <s:enumeration value="ARS"/>
   <s:enumeration value="AWG"/>
   <s:enumeration value="AUD"/>
 </s:restriction>
</s:simpleType>
<s:element name="ConversionRateResponse">
  <s:complexType>
  <s:sequence>
  <s:element minOccurs="1" maxOccurs="1" name="ConversionRateResult" type="s:double"/>
  </s:sequence>
  </s:complexType>
  </s:element>
  <s:element name="double" type="s:double"/>
  </s:schema>
</wsdl:types>

我的 Android 类:

public class MainActivity extends Activity {

 private static final String NAMESPACE = "http://www.webserviceX.NET";

private static String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx?WSDL"; 
private static final String METHOD_NAME = "ConversionRate";
private static final String SOAP_ACTION =  "http://www.webserviceX.NET/ConversionRate";
 private TextView lblResult;   


 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  lblResult = (TextView) findViewById(R.id.result);

  SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 


  PropertyInfo propInfo=new PropertyInfo();
  propInfo.name="FromCurrency";
  propInfo.type=PropertyInfo.STRING_CLASS;
  propInfo.setValue("AFA");


  PropertyInfo propInfo2 = new PropertyInfo();
  propInfo2.name="ToCurrency";
  propInfo2.type = PropertyInfo.STRING_CLASS;
  propInfo2.setValue("AUD");

  request.addProperty(propInfo);
  request.addProperty(propInfo2);

  SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 

  envelope.setOutputSoapObject(request);
  envelope.dotNet=true;

  HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

  try {
   androidHttpTransport.call(SOAP_ACTION, envelope);

   SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

   lblResult.setText(resultsRequestSOAP.toString());


  } catch (Exception e) {
      lblResult.setText(e.getMessage());

  }

 }

有人对错误有什么建议吗?

谢谢!

丹尼尔

最佳答案

我认为您的 URL 字符串是:

private static String URL = "http://www.webserviceX.NET/CurrencyConvertor.asmx";

然后试试这个:

Object resultsRequestSOAP = envelope.getResponse();

而不是这个:

SoapPrimitive  resultsRequestSOAP = (SoapPrimitive) envelope.getResponse();

关于android - 使用 KSOAP 的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8544314/

相关文章:

web-services - Apache CXF 与 Apache Camel

jquery - 使用 jQuery 调用远程 ASMX 的问题

java - 将 ksoap2 响应解析为 int 数组

android - ListView 中的 TextField 可聚焦性

java - 如何将 JSONArray 中的字节复制到字节数组

java - Android在 fragment 中获取图像的文件路径

android - Android 中的 KSOAP 网络服务

android - 使用支持库时如何在 ListView 中提供 ContextMenu?

java - 如何从 Java 调用 .net webservice

Android 1.6 ksoap2 "RuntimeException: Cannot serialize: java.util.GregorianCalendar..",同时传递日期时间参数