连接Web服务时出错(soap)

时间:2015-04-16 11:33:43

标签: android web-services

package com.example.soapconnect;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AsyncCallWS task = new AsyncCallWS();
        task.execute(); 


        //client = new DefaultHttpClient();            
        //new Read().execute("text");
 }


    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
        @Override
        protected Void doInBackground(Void... params) {
           calculate();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

        }

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onProgressUpdate(Void... values) {

        }

    }

    public void calculate() 
    {
        final String OPERATION_NAME = "currDateTime";// your webservice web method name
        final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
        final String SOAP_ADDRESS = "https://taxi.bharathtravels.com/WebService.asmx";
        SOAP_ACTION = WSDL_TARGET_NAMESPACE + OPERATION_NAME;

        try { 


             SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
             PropertyInfo propertyInfo = new PropertyInfo();
             propertyInfo.type = PropertyInfo.STRING_CLASS;
             propertyInfo.name = "eid";
             edata =(EditText)findViewById(R.id.editText1);
             studentNo=edata.getText().toString();
           //  request.addProperty(propertyInfo, studentNo);
             SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
             SoapEnvelope.VER11);
             envelope.dotNet = true;
             envelope.setOutputSoapObject(request);
             HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
             httpTransport.call(SOAP_ACTION, envelope);  
             Object response = envelope.getResponse();                    
             tvData1.setText(response.toString());


           // Log.i(TAG, "Result Celsius: " + resultString);
        }
        catch(Exception ex) {
            tvData1.setText(ex.toString()+"  Or enter number is not Available!");  
        }

        tvData1 = (TextView)findViewById(R.id.textView1);
    }
}

我是android的新手,我正在尝试使用Web服务来传递.net代码。我尝试了很多解决方案,但是我在

中遇到了错误
httpTransport.call(SOAP_ACTION, envelope); 

我很感激任何帮助解决这个问题,因为我无法弄明白。

1 个答案:

答案 0 :(得分:0)

添加以下行:

   setContentView(R.layout.activity_main);
    if (android.os.Build.VERSION.SDK_INT > 9)
    {
         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
         StrictMode.setThreadPolicy(policy);
    }
<{1>}

下的

并且对于ssl Exception,您可以尝试:

setContentView(R.layout.activity_main);