简体   繁体   中英

Call ASP.NET web service using android using ksoap

I created a simple class for call web service

public class CallSoap {
    public final String SOAP_ACTION = "http://tempuri.org/Add";

    public  final String OPERATION_NAME = "Add"; 

    public  final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";

    public  final String SOAP_ADDRESS = "http://localhost:41614/Service1.asmx";
    public CallSoap() 
    { 
    }
    public String Call(int a,int b)
    {
    SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
    request.addProperty("a",a);
    request.addProperty("b",b);

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

    HttpTransport httpTransport = new HttpTransport(SOAP_ADDRESS);
    Object response=null;
    try
    {
    httpTransport.call(SOAP_ACTION, envelope);
    response = envelope.getResponse();
    }
    catch (Exception exception)
    {
    response=exception.toString();
    }
    return response.toString();
    }
}

This is my Activity click method

public void clickData(View v)
{
    //Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText et=(EditText) findViewById(R.id.editText1);
    EditText et2=(EditText) findViewById(R.id.editText2);
    TextView tv=(TextView) findViewById(R.id.textView1);

    int a=Integer.parseInt(et.getText().toString());
    int b=Integer.parseInt(et2.getText().toString()); 

    CallSoap cs=new CallSoap();
    tv.setText(cs.Call(a, b));
}

There is an error "Unportunately App1 has sttoped". Please help me..

这是由于Android 2.2之后出现NetworkOnMainThread异常,您无法在mainUi线程中运行长时间运行的操作(例如网络访问),因为mainUI线程用于加载Ui组件以使您的应用程序流畅高效。您应该使用AsynTask类来保持它脱离主线程

For starters, and also as a very good practice, make the call into a separate thread. I do it like this:

public void call() {
    Log.d("TAG", "Started call");
    new Thread(new Runnable()
    {
        public void run() {
            Looper.prepare();

            /** UI stuff/ This runs on the main thread to update the UI. This can be used for displaying something like "please wait */

            AppPrefFragment.updateSyncJobsNumber(AppPrefFragment.JOB_START);
            if (AppPrefActivity.context != null) {
                ((Activity) AppPrefActivity.context).runOnUiThread(new Runnable()
                {
                    public void run() {
                        //AppPrefFragment.setSyncPreferenceStatusShowingOngoingJobs();
                        ... show "please wait"
                    }
                });
            }


            /** Actual stuff  that you want to do */

            ... the HttpCall or SOAP etc
        }
    }).start();
}

Please note that the return has to be handled asynchronously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM