使用ksoap使用android调用ASP.NET web服务

使用ksoap使用android调用ASP.NET web服务,android,asp.net,web-services,android-ksoap2,Android,Asp.net,Web Services,Android Ksoap2,我为callweb服务创建了一个简单的类 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 fina

我为callweb服务创建了一个简单的类

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();
    }
}
这是我的
活动
单击方法

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));
}

有一个错误“不恰当的App1已停止”。请帮助我..

这是由于android 2.2之后出现NetworkOnMainThread异常,您无法在mainUi线程中运行长时间运行的操作,例如网络访问,由于mainUI线程用于加载Ui组件以使应用程序平滑高效。对于初学者,您可能应该使用AsynTask类使其远离主线程,并且作为一种非常好的实践,将调用放入单独的线程中。 我是这样做的:

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();
}

请注意,返回必须异步处理。

post logcat详细信息。还可以查看链接。这应该对你有帮助。您正在使用的安卓操作系统是什么?我猜是这样的情况,你可以记录和共享异常[使用Logcat->log.e(标签,消息)]?Im使用android 4.2版本捕获意外网络访问或磁盘写入你应该使用严格模式启用