2013-02-20 178 views
0

試圖在Android中調用WCF方法,它在 androidHttpTransport.call(...) 處停止並且過了一段時間後,由於無法連接到服務器,它有超時。 下面是代碼:Android未能連接到WCF服務器

我的Android代碼:

public class MainActivity extends Activity { 
private static final String NAMESPACE = "http://try1.com/"; 
private static final String METHOD_NAME = "ObtainAnswerToQuestion"; 
private static final String SOAP_ACTION = "http://bcaa.com/IEightBall/ObtainAnswerToQuestion"; 
private static final String URL = "http://10.0.2.2:8080/MagicEightBallService"; 
Button submit; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    submit = (Button)this.findViewById(R.id.submit); 
    submit.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);      
      SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);     
      envelope.dotNet = true; 
      PropertyInfo info = new PropertyInfo(); 
      info.setName("Question"); 
      info.setType("Bylawme".getClass()); 
      info.setValue("BB2233asdsa"); 
      request.addProperty(info); 
      envelope.setOutputSoapObject(request); 
      new LongProcess().execute(envelope); 
     } 
    }); 
} 
private class LongProcess extends AsyncTask<Object, Void, Boolean> { 

     @Override 
     protected Boolean doInBackground(Object... param) { 
      HttpTransportSE androidHttpTransport = new HttpTransportSE(URL,30000); 
      SoapSerializationEnvelope envelope = (SoapSerializationEnvelope) param[0]; 
      try { 
       androidHttpTransport.call(SOAP_ACTION, envelope); 
       SoapPrimitive result = (SoapPrimitive) envelope.getResponse(); 
       // to get the data 
       String resultData = result.toString(); 
       submit.setText(resultData); 
      } catch (Exception ex) { 
       submit.setText(ex.getMessage()); 
      } 
      return true; 
     }  
     @Override 
     protected void onPostExecute(Boolean result) { 
     } 
     @Override 
     protected void onPreExecute() { 
     } 
     @Override 
     protected void onProgressUpdate(Void... values) { 
     } 
    } 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 
    } 

服務器WSDL:

<?xml version="1.0" encoding="UTF-8"?> 
     -<wsdl:definitions name="MagicEightBallService"     xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:i0="http://try1.com" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/"> 
    <wsdl:import location="http://localhost:8080/MagicEightBallService?wsdl=wsdl0" namespace="http://try1.com"/> 
    <wsdl:types/> 
    -<wsdl:binding type="i0:IEightBall" name="BasicHttpBinding_IEightBall"> 
     <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> 
     -<wsdl:operation name="ObtainAnswerToQuestion"> 
      <soap:operation style="document" soapAction="http://try1.com/IEightBall/ObtainAnswerToQuestion"/> 
      -<wsdl:input> 
       <soap:body use="literal"/> 
      </wsdl:input> 
      -<wsdl:output> 
       <soap:body use="literal"/> 
      </wsdl:output> 
     </wsdl:operation> 
     </wsdl:binding> 
    -<wsdl:service name="MagicEightBallService"> 
     -<wsdl:port name="BasicHttpBinding_IEightBall" binding="tns:BasicHttpBinding_IEightBall"> 
      <soap:address location="http://localhost:8080/MagicEightBallService"/> 
     </wsdl:port> 
     </wsdl:service> 
    </wsdl:definitions> 

任何幫助將不勝感激。

+0

該調用是否被調度到WCF服務?你能從運行客戶端的機器訪問(瀏覽器)服務嗎?你的服務如何託管? – Jocke 2013-02-20 09:49:50

回答

0

已解決問題。這是因爲2問題:

(1)發現從另一個問題,這個解決方案在這個論壇(現在無法找到它...):

require to kill a connection when not in use, by adding in the following code 

    System.setProperty("http.keepAlive", "false"); 

,並保持當前連接活着包括本線之前的

androidHttpTransport.call(...): 
     androidHttpTransport.getServiceConnection().setRequestProperty("Connection","Keep-Alive"); 

(2)不應該叫answer.setText(...)LoopProcess線程。所以只需要將值返回到主線程來設置值。

and viola it works,finally!