0

我正在尝试在 Android 中使用 ksoap2 调用基于 .net 的 WCF 服务。进行 httptransport 调用时出现以下错误。

这是我的代码:

public class MainActivity extends Activity {
EditText t1,t2;
Button b1;
TextView result;

private static final String NAMESPACE="http://tempuri.org/";
private static final String METHOD_NAME="AddEmployee";
private static final String URL="http://10.32.4.24/Android/AndroidBus.svc";
private static final String SOAP_ACTION="http://tempuri.org/IAndroidBus/AddEmployee";
SoapObject request=new SoapObject(NAMESPACE,METHOD_NAME);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        t1=(EditText)findViewById(R.id.editText1);
        t2=(EditText)findViewById(R.id.editText2);
        result=(TextView)findViewById(R.id.textView1);
        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
            PropertyInfo pi=new PropertyInfo();
            pi.setValue("abc");
            pi.setName("value1");
            //pi.type=PropertyInfo.STRING_CLASS;
            SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);

            Request.addProperty(pi);

            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
            envelope.dotNet=true;
            envelope.setOutputSoapObject(Request);

             new servicecall(envelope).execute();



            }

        });
    }

    public class servicecall extends AsyncTask<String,Void,String>
    {
        String res;
        SoapSerializationEnvelope envelope;
        public servicecall(SoapSerializationEnvelope envelope)
        {
            this.envelope=envelope;
        }

        @Override
        protected String doInBackground(String... arg0) {
            HttpTransportSE ht=new HttpTransportSE(URL);
            ht.debug=true;  
            try
            {
                Log.d("WEBSERVICE","Calling");
               ht.call(SOAP_ACTION,envelope);
              SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
              String dump=ht.responseDump.toString();
              System.out.println("dumppp"+dump);
               Log.d("Tag","Response"+response);
               res=response.toString();

            }
            catch(Exception e)
            {
                e.printStackTrace();

            }

            return res;
        }

        protected void onPostExecute(String res) {
            super.onPostExecute(res);
            result.setText("completed");

    }
    }

这是我得到的错误:

11-08 15:00:24.421: W/System.err(767): java.lang.NullPointerException
11-08 15:00:24.451: W/System.err(767):  at org.ksoap2.transport.ServiceConnectionSE.getResponseProperties(ServiceConnectionSE.java:85)
11-08 15:00:24.473: W/System.err(767):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:187)
11-08 15:00:24.483: W/System.err(767):  at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:114)
11-08 15:00:24.504: W/System.err(767):  at com.example.androidservice.MainActivity$servicecall.doInBackground(MainActivity.java:81)
11-08 15:00:24.522: W/System.err(767):  at com.example.androidservice.MainActivity$servicecall.doInBackground(MainActivity.java:1)
11-08 15:00:24.533: W/System.err(767):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-08 15:00:24.541: W/System.err(767):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-08 15:00:24.661: W/System.err(767):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-08 15:00:24.671: W/System.err(767):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-08 15:00:24.685: W/System.err(767):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-08 15:00:24.711: W/System.err(767):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-08 15:00:24.722: W/System.err(767):  at java.lang.Thread.run(Thread.java:856)
4

3 回答 3

0

确保您收到的响应不为空。

    SoapPrimitive response = (SoapPrimitive)envelope.getResponse();
    if(null==response){ 
        return;
    }
    else{
        //continue processing
    }

工作 KSOAP 代码

于 2012-11-08T10:22:35.613 回答
0

请阅读这篇文章,它解释了你必须做的所有事情。您可以重新启动代码。有用。http://insightforfuture.blogspot.com/2012/02/accessing-apache-axis2-web-service_24.html

于 2012-11-10T03:57:52.920 回答
0

只需在按钮单击时调用后台服务并更改 doInBackground 方法中的代码,如下所示

                  SoapObject request = new SoapObject(NAMESPACE, methode);
        // Add the input required by web service

        request.addProperty("abc", value1);



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

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);

        httpTransport.call(Action, envelope);

        Object response = envelope.getResponse();

        Log.v("Responce", "Responce:::" + response.toString());

        result = response.toString();
于 2012-11-08T09:56:13.160 回答