2013-04-30 36 views
0

我有當我嘗試通過Android應用程序調用Web服務在這個異常訪問Web服務有錯誤通過Android應用

它顯示了以下情況除外

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) 

我的web服務代碼

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; 
    } 

任何人都可以幫助我的代碼,任何人都可以告訴我我錯了嗎?

回答

0

嘗試:

最終字符串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()); 
+0

得到這個錯誤.. 常量不能被解析爲一個可變 – 2013-04-30 08:25:30

+0

更換線envelope.xsd = Constants.NAME_SPACE;使用envelope.xsd = NAME_SPACE; – deepak825 2013-04-30 10:08:00

+0

java.io.IOException:HTTP請求失敗,HTTP狀態:500 它現在拋出此異常。 然後應用程序崩潰。 – 2013-04-30 10:55:16