使用Android 4.0.3使用kso​​ap 2.6.0访问Web服务

时间:2012-02-01 18:49:45

标签: android soap ksoap2

我正在尝试让我的Android手机访问IDEOne(www.ideone.com)网络服务,因此我可以将其API用于我正在进行的这个项目。可以使用kSoap访问webservice,所以我使用的是从Google网站获得的kSoap 2.6.0。我的代码在以下代码行中抛出异常:

httpTransport.call(SOAP_ACTION,envelope); //发送请求

我检查了无线连接并使用了其他无线连接,以确保它不是防火墙问题。我检查了Android模拟器上的连接,并且有连接。我只是通过kSoap(http://www.helloandroid.com/tutorials/using-ksoap2-android-and-parsing-output-data)运行其他人在线发布的关于将Android连接到Web服务的代码,但没有运气。它在同一个调用函数上抛出异常。有什么建议吗?以下是我的代码。预先感谢您的帮助。

公共类IDEOneStubI {

private static final String METHOD_NAME = "createSubmission";
private static final String SOAP_ACTION = "http://ideone.com/api/1/service#createSubmission";
private static final String NAMESPACE = "http://ideone.com/api/1/serviceL";
private static final String URL = "http://ideone.com/api/1/service;";

//您可以从wsdl文件中获取这些值^

public SoapObject soap(String METHOD_NAME, String SOAP_ACTION, String NAMESPACE, String URL) throws IOException, XmlPullParserException {
   SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); //set up request
    request.addProperty("user", "nsbradley"); //variable name, value. I got the variable name, from the wsdl file!
    request.addProperty("pass", "12345678");
    request.addProperty("sourceCode", "Hello World");
    request.addProperty("language", 10);
    request.addProperty("input", "");
    request.addProperty("run", Boolean.TRUE);
    request.addProperty("private", Boolean.FALSE);
   SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); //put all required data into a soap envelope
   envelope.dotNet = true;
   envelope.setOutputSoapObject(request);  //prepare request
   HttpTransportSE httpTransport = new HttpTransportSE(URL);  
   httpTransport.debug = true;  //this is optional, use it if you don't want to use a packet sniffer to check what the sent message was
   httpTransport.call(SOAP_ACTION, envelope); //send request
   SoapObject result=(SoapObject)envelope.getResponse(); //get response
   return result;
}

3 个答案:

答案 0 :(得分:0)

我认为您的网址字符串错误,请在结尾处删除分号,然后重试。

答案 1 :(得分:0)

从Android 3.0开始,您必须从异步任务中调用此类方法。

答案 2 :(得分:0)

首先确保在清单文件中将互联网添加到互联网

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

如果不起作用,请尝试更改

 SoapObject result=(SoapObject)envelope.getResponse(); //get response

 SoapObject result=(SoapObject)envelope.bodyIn; //get response

此外,您应该将webservice调用放在try catch块中,或抛出SoapFault异常。