android中的Webservice调用错误

时间:2012-11-08 09:41:39

标签: android wcf web-services

我试图在Android中使用kso​​ap2调用基于.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)

3 个答案:

答案 0 :(得分:0)

只需点击按钮调用后台服务,然后更改doInBackground metode中的代码,如下所示

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

答案 1 :(得分:0)

确保您收到的回复不为空。

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

Working KSOAP code

答案 2 :(得分:0)

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