0

尝试在 Android 中调用 WCF 方法,它在 androidHttpTransport.call(...) 处停止,一段时间后,由于无法连接到服务器而超时。以下是代码:

我的安卓代码:

    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>

任何帮助将不胜感激。

4

1 回答 1

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线程中调用。所以只需要将值返回给主线程来设置值。

和中提琴它的作品,终于!

于 2013-02-21T03:28:52.177 回答