2012-08-11 43 views
1
.NET Web服務時發生

我已經開發的.NET Web服務,這是沒有調用Android的

[WebService(Namespace = "http://tempuri.org/")] 
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
[System.ComponentModel.ToolboxItem(false)] 
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService] 
public class DataManipulationService : System.Web.Services.WebService 
{ 
    [WebMethod] 
    public void InsertData() 
    { 
     SQLConnectionManager conn = new SQLConnectionManager(); 

     string qry = "INSERT INTO TEST_TABLE(Id, Name) VALUES(3, 'Ibbi')"; 

     SQLOperationManager op = new SQLOperationManager(qry); 
     int rowsAffected = op.ExecuteQuery(); 
    } 
} 

我試圖打電話給在Android這個網站的方法,這樣

public class WebDataManipulator { 

private final String NAMESPACE = "http://tempuri.org/"; 
private final String URL = "http://localhost/DataManipulationService.asmx?WSDL"; 
private final String METHOD = "InsertData"; 
private final String SOAP_ACTION = "http://localhost/DataManipulationService.asmx?op=InsertData"; 

public void insertData() { 
    SoapObject request = new SoapObject(NAMESPACE, METHOD); 

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

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


    try { 
     androidHttp.call(SOAP_ACTION, envelope); 
    } catch(Exception ex) { 
     ex.printStackTrace(); 
    } 
} 

}

我在.NET測試我的web服務,其工作正常,但當我在Android上運行NOTHING發生這種情況..無論我得到任何異常,也沒有它的運作。爲什麼? 這是我的logcat

08-11 11:09:12.269: I/AndroidRuntime(325): NOTE: attach of thread 'Binder Thread #3' failed 
08-11 11:09:13.179: I/System.out(333): InsertData{} 
08-11 11:09:13.229: W/System.err(333): java.net.SocketException: Permission denied 
08-11 11:09:13.239: W/System.err(333): at org.apache.harmony.luni.platform.OSNetworkSystem.socket(Native Method) 
08-11 11:09:13.239: W/System.err(333): at dalvik.system.BlockGuard$WrappedNetworkSystem.socket(BlockGuard.java:335) 
08-11 11:09:13.239: W/System.err(333): at org.apache.harmony.luni.net.PlainSocketImpl.create(PlainSocketImpl.java:216) 
08-11 11:09:13.249: W/System.err(333): at java.net.Socket.checkOpenAndCreate(Socket.java:802) 
08-11 11:09:13.249: W/System.err(333): at java.net.Socket.connect(Socket.java:948) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 
08-11 11:09:13.249: W/System.err(333): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 
08-11 11:09:13.249: W/System.err(333): at org.ksoap2.transport.ServiceConnectionSE.connect(ServiceConnectionSE.java:76) 
08-11 11:09:13.249: W/System.err(333): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:146) 
08-11 11:09:13.249: W/System.err(333): at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:95) 
08-11 11:09:13.249: W/System.err(333): at com.automated.research.WebDataManipulator.insertData(WebDataManipulator.java:27) 
08-11 11:09:13.259: W/System.err(333): at com.automated.research.DataSender$1.run(DataSender.java:47) 
08-11 11:09:13.259: I/System.out(333): connected 

,這是我怎麼寫客戶端代碼

if(isNetworkAvailable()) { 
    WebDataManipulator webDM = new WebDataManipulator(); 
    webDM.insertData(); 
    System.out.println("connected"); 
} 
+0

顯示代碼和logcat? – t0mm13b 2012-08-11 01:32:16

+0

@ t0mm13b感謝您的回覆,我編輯了我的問題幷包含logcat。 – Ibad 2012-08-11 06:34:49

回答

0

我會在這裏回答我自己的問題,我正在寫我爲了讓它工作的步驟。

  1. 請確保您在IIS中正確託管了web服務。你可以參考這個link

    託管服務,如果你看到的服務是不是你的機器上訪問,然後按照步驟2

  2. 你需要在這裏做的兩件事情,

    1. 製作asp.net後出現IIS經理。請參閱link here
    2. 修改web.config文件並添加httpGet和httpPost。 Example here
  3. 允許該服務從遠程機器調用其webmethods。請參閱link here

  4. 確保防火牆不阻止訪問您的計算機。您可以定義新規則,或者禁用防火牆以查看這些規則。

按照上述步驟,我現在可以在Android設備上調用我的服務。

1

的webserviceurl開始與「本地主機」,這是從視Android設備的Android點。替換本地主機的Android設備可以看到的IP地址(即可從Android網絡瀏覽器調用)

+0

嗨@ k3b感謝您的回答。我嘗試用我的計算機的IP地址替換本地主機,但現在得到以下異常 不支持java.net.socketexception地址系列 – Ibad 2012-08-11 07:04:04

+0

可以從您的Android瀏覽器調用Web服務'http://servername/DataManipulationService.asmx?WSDL' ? – k3b 2012-08-11 07:28:17

+0

沒有k3b它沒有被從android瀏覽器調用:(但是,當我用ip '10 .0.2.2'替換'localhost'並且在模擬器上運行時它然後工作得很好。但是它在設備上不工作 – Ibad 2012-08-11 07:56:33

相關問題