通过Android应用程序访问Web服务时出错

时间:2013-04-30 06:29:01

标签: android web-services android-ksoap2 primitive ksoap

当我试图通过Android应用程序调用web服务时,我得到了这个例外

它显示以下例外情况

04-30 11:51:06.558: E/Error :(297): Error on soapPrimitiveData() Transport endpoint is not connected
04-30 11:51:06.577: W/System.err(297): java.net.SocketTimeoutException: Transport endpoint is not connected
04-30 11:51:06.587: W/System.err(297):  at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocketImpl(Native Method)
04-30 11:51:06.587: W/System.err(297):  at org.apache.harmony.luni.platform.OSNetworkSystem.connectStreamWithTimeoutSocket(OSNetworkSystem.java:130)
04-30 11:51:06.597: W/System.err(297):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:246)
04-30 11:51:06.597: W/System.err(297):  at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533)
04-30 11:51:06.607: W/System.err(297):  at java.net.Socket.connect(Socket.java:1055)
04-30 11:51:06.607: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:62)
04-30 11:51:06.617: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:88)
04-30 11:51:06.617: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHTTPConnection(HttpURLConnectionImpl.java:927)
04-30 11:51:06.617: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:909)
04-30 11:51:06.628: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.sendRequest(HttpURLConnectionImpl.java:1325)
04-30 11:51:06.628: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequestInternal(HttpURLConnectionImpl.java:1656)
04-30 11:51:06.628: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.doRequest(HttpURLConnectionImpl.java:1649)
04-30 11:51:06.637: W/System.err(297):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:1243)
04-30 11:51:06.637: W/System.err(297):  at org.ksoap2.transport.ServiceConnectionSE.openOutputStream(ServiceConnectionSE.java:126)
04-30 11:51:06.647: W/System.err(297):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:179)
04-30 11:51:06.647: W/System.err(297):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:116)
04-30 11:51:06.647: W/System.err(297):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:111)
04-30 11:51:06.657: W/System.err(297):  at com.practice.locationtracking.MainActivity.sendLocation(MainActivity.java:137)
04-30 11:51:06.667: W/System.err(297):  at com.practice.locationtracking.MainActivity.access$0(MainActivity.java:120)
04-30 11:51:06.667: W/System.err(297):  at com.practice.locationtracking.MainActivity$1.run(MainActivity.java:106)
04-30 11:51:06.678: W/System.err(297):  at android.os.Handler.handleCallback(Handler.java:587)
04-30 11:51:06.678: W/System.err(297):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-30 11:51:06.687: W/System.err(297):  at android.os.Looper.loop(Looper.java:123)
04-30 11:51:06.687: W/System.err(297):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-30 11:51:06.697: W/System.err(297):  at java.lang.reflect.Method.invokeNative(Native Method)
04-30 11:51:06.697: W/System.err(297):  at java.lang.reflect.Method.invoke(Method.java:521)
04-30 11:51:06.697: W/System.err(297):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-30 11:51:06.707: W/System.err(297):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-30 11:51:06.717: W/System.err(297):  at dalvik.system.NativeStart.main(Native Method)

我的网络服务代码

Imports System.Web
Imports System.Web.Services
Imports System.Data
Imports System.Web.Services.Protocols
Imports System.Data.SqlClient

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class WebService
    Inherits System.Web.Services.WebService
    Dim conn As New SqlConnection


    <WebMethod()> _
    Public Function HelloWorld() As String
        Return "Hello World"
    End Function

    'Public Function SetLocationWebUser(ByVal sLattitude As Double, ByVal sLongitude As Double, ByVal sAddress As String, ByVal sLocality As String, ByVal sCountry As String, ByVal sUserId As String) As Boolean
    Public Function addLocationData(ByVal Latitude As String, ByVal Longitude As String, ByVal UserName As String) As Boolean
        Try
            Dim sdr As New DataSet()
            Dim dt As New DataTable()

            'conn.ConnectionString = "Data Source = 62.75.229.51; Initial Catalog = Silver_WebPH; User id =Silver_Webph; password =sbyadmin"
            'Dim strSql As String = "INSERT INTO MAP (LATITUDE,LONGITUDE,SERVER_DATE,ADDRESS,LOCALITY,COUNTRY,USER_ID,WEB_ENTRY) VALUES (" + sLattitude.ToString + "," + sLongitude.ToString + ",getdate(),'" + sAddress.Replace("'", "''") + "','" + sLocality.Replace("'", "''") + "','" + sCountry.Replace("'", "''") + "','" + sUserId + "',1 )"
            conn.ConnectionString = "Data Source=KunalShah-PC;Initial Catalog=Location;Integrated Security=True"
            Dim strsql As String = "INSERT INTO LocatioData values (@Longitude, @Latitide, @UserName)"

            conn.Open()
            Dim cmd As New SqlCommand
            cmd.Connection = conn
            cmd.CommandText = strsql
            cmd.CommandType = CommandType.Text
            cmd.Parameters.Add("@Longitude", SqlDbType.NChar).Value = Longitude
            cmd.Parameters.Add("@Latitude", SqlDbType.NChar).Value = Latitude
            cmd.Parameters.Add("@UserName", SqlDbType.NChar).Value = UserName

            cmd.ExecuteNonQuery()
            Return True

        Catch ex As Exception
            Return False
        End Try
    End Function
End Class

从我的Android应用程序发送位置的功能如下..

private boolean sendLocation(String latitude, String longitude, String UserName)
    {
        boolean flag = false;
        final String SOAP_ACTION = "http://tempuri.org/addLocationData";
        final String METHOD_NAME = "addLocationData";
        SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);

        request.addProperty("Latitude",latitude);
        request.addProperty("Longitude", longitude);
        request.addProperty("UserName", UserName);
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

        System.out.println(request);
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try
        {
            androidHttpTransport.call(SOAP_ACTION, envelope);
            SoapPrimitive result= (SoapPrimitive)envelope.getResponse();
            String res_string = result.toString();
            System.out.println(res_string);
        }
        catch(SocketException ex)
        {
            Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage());
            ex.printStackTrace();
        }
        catch(Exception e)
        {
            Log.e("Error : " , "Error on soapPrimitiveData() " + e.getMessage());
            e.printStackTrace();
        };
        return flag;
    }

任何人都可以帮我代码,任何人都可以告诉我哪里错了吗?

1 个答案:

答案 0 :(得分:0)

尝试这个:

final String SOAP_ACTION =“http://tempuri.org/addLocationData”;

final String METHOD_NAME = "addLocationData";
SoapObject request = new SoapObject (NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
        SoapEnvelope.VER11);
envelope.dotNet = true;     
envelope.xsd = Constants.NAME_SPACE;
envelope.enc = "http://schemas.datacontract.org/2004/07/Entity";
 request.addProperty("Latitude",latitude);
request.addProperty("Longitude", longitude);
request.addProperty("UserName", UserName);

envelope.setOutputSoapObject(request);
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(
        URL, 60000);
androidHttpTransport
        .setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

try {
    androidHttpTransport.call(NAMESPACE, METHOD_NAME, envelope);
    } 
catch (Exception e)
    {
    e.printStackTrace();
    }

SoapObject result = (SoapObject) envelope.bodyIn;
Log.e("result", "" + result.toString());