Android后台服务ksoap错误

时间:2016-04-20 05:04:43

标签: android web-services android-service ksoap2

我正在开发一个带有后台服务的Android应用程序,它每5分钟就会使用一个Web服务方法。我正在使用kso​​ap 2.4和asp.net web服务。 Android后台服务每5分钟运行一次,但

 androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);

返回null。 这是我的android服务代码。

package com.example.rashid.challandb;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.MarshalBase64;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
public class TestService extends Service {
private static String NAMESPACE = "http://tempuri.org/";
private static String URL = "http://192.168.50.107:80/AndroidService/Service1.asmx";
private static String SOAP_ACTION = "http://tempuri.org/";
Context context;
boolean isrunning;
public TestService() {
}
@Override
public  void onCreate()
{
    this.context=this;
    this.isrunning=false;
}
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent,int flags,int startid) {
    if (!this.isrunning) {
        this.isrunning = true;
        synchronized (this) {
            try {
                String resTxt = "default";
                String webMethName="MyMethod";
                SoapObject request = new SoapObject(NAMESPACE, webMethName);

                PropertyInfo first = new PropertyInfo();
                first.setName("Para");
                first.setValue("Hullow");
                first.setType(String.class);
                request.addProperty(first);

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

                HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                try {

                    androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);
                    Object response = null;
                    response = (Object) envelope.getResponse();
                    resTxt =response.toString();

                } catch (Exception e) {
                    e.printStackTrace();
                    System.out.println(   e.getMessage()+" Phone");
                }

                System.out.println(  resTxt);

            } catch (Exception ex) {
                System.out.println("Error:" + ex.getMessage());
            }
            this.isrunning = false;
            stopSelf();
        }
    }
    return START_STICKY;
}
@Override
public void onDestroy()
{
    this.isrunning=false;
}
}

这是wsdl

<wsdl:definitions xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="MyMethod">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="Para" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="MyMethodResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MyMethodResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message name="MyMethodSoapIn">
<wsdl:part name="parameters" element="tns:MyMethod"/>
</wsdl:message>
<wsdl:message name="MyMethodSoapOut">
<wsdl:part name="parameters" element="tns:MyMethodResponse"/>
</wsdl:message>
<wsdl:portType name="Service1Soap">
<wsdl:operation name="MyMethod">
<wsdl:input message="tns:MyMethodSoapIn"/>
<wsdl:output message="tns:MyMethodSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="Service1Soap" type="tns:Service1Soap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="MyMethod">
<soap:operation soapAction="http://tempuri.org/MyMethod" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="Service1Soap12" type="tns:Service1Soap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="MyMethod">
<soap12:operation soapAction="http://tempuri.org/MyMethod" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service1">
<wsdl:port name="Service1Soap" binding="tns:Service1Soap">
<soap:address location="http://192.168.50.107/AndroidService/Service1.asmx"/>
</wsdl:port>
<wsdl:port name="Service1Soap12" binding="tns:Service1Soap12">
<soap12:address location="http://192.168.50.107/AndroidService/Service1.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

1 个答案:

答案 0 :(得分:0)

尝试替换

androidHttpTransport.call(SOAP_ACTION+webMethName, envelope);

用这个

androidHttpTransport.call("", envelope);

即使这没有解决, 请检查此行中的异常退货

System.out.println("Error:" + ex.getMessage());

修改

SoapObject request = new SoapObject(NAMESPACE, webMethName);
request.addProperty("Para", "Hullow");

删除propertyInfo部分