2012-12-10 41 views
2

我想從Android應用調用asmx Web服務。今天從字面上開始了一些Android開發。我一直在關注我在網上和在這裏找到的各種解決方案,看起來比預期更困難。我嘗試了各種解決方案,並且使用KSoap2似乎是最簡單的方法來實現這一點,以及如果我能得到它的工作。從Android應用調用Web服務似乎掛起

我有下面的代碼工作,直到一個觀點:

private class CallWebService extends AsyncTask<Void, Void, Void> { 

    private static final String SOAP_ACTION = "http://tempuri.org/GetUser"; 
    private static final String METHOD_NAME = "GetUser"; 
    private static final String NAMESPACE = "http://tempuri.org/"; 
    private static final String URL = "http://160.10.1.79:59315/Service1.asmx"; 
    TextView tv; 

    @Override 
    protected Void doInBackground(Void... params) { 
     tv=(TextView)findViewById(R.id.txtMessage); 

     try { 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 

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

      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 

      androidHttpTransport.call(SOAP_ACTION, envelope); 

      Object result = (Object)envelope.getResponse(); 

      tv.setText(result.toString()); 
     } 
     catch (Exception e) { 
      tv.setText(e.getMessage()); 
     } 
     return null; 
    } 
} 

似乎在該行androidHttpTransport.call(SOAP_ACTION, envelope);任何想法,爲什麼掛?這是正確的方法嗎?我應該朝另一個方向看嗎?

+0

你是什麼意思它掛起?它掛在那裏幾秒鐘或無限期地? –

+0

無限期掛起。不會拋出超時異常或任何異常。我只需要停止它手動運行。我將嘗試更新版本的KSoap2,看看它是否有所作爲。我認爲我使用的版本是從2006年開始的! – anothershrubery

回答

1

確定這種情況的解決方案是在年底相當簡單。 Web服務在localhost下運行,但不能使用localhost:59315。所以我把它改爲**MY IP**:59315。仍然沒有工作。使用這個問題Accessing localhost:port from Android emulator我嘗試使用10.0.2.2:59315,它工作。非常感謝Akhil Jain在另一個問題上。

1
tv.setText(result.toString()); 

這是不是在所有建議從doInBackground訪問UI。

doInBackground會自動傳遞結果給onPostExecute,你可以設置文本有

protected void onPostExecute(String result){ 
    tv.setText(result.toString()); 
} 
+0

這不是一個真正的問題,它是一個測試應用程序,只是爲了功能目的而進行測試。 「不推薦」我明白,但這與我的問題無關,因爲這條線甚至沒有被擊中! – anothershrubery

相關問題