Question

I have a problem when reading a response from .NET web service by an Android client. The response structure is (example):

<xml>
    <resaRef>400</resaRef>
    <firstname>Amine</name>
    <lastname>Touahria</lastname>
</xml>

Here is my code :

        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.app.Activity;
        import android.os.AsyncTask;
        import android.os.Bundle;
        import android.view.View;
        import android.view.View.OnClickListener;
        import android.widget.Button;
        import android.widget.EditText;

        public class MainActivity extends Activity {
            EditText ref;
            Button call;
            public final String NAMESPACE = "http://tempuri.org/";
            public final String URL = "http://192.168.0.100/hngwebsetup/HNGWeb.asmx";
            public final String SOAP_ACTION = "http://tempuri.org/GetResa";
            public final String METHOD_NAME = "GetResa";
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                ref=(EditText)findViewById(R.id.ref_res);


                call=(Button)findViewById(R.id.call_ws);
                call.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        AsyncCallWS task = new AsyncCallWS();
                        task.execute();

                    }
                });

            }
            public class AsyncCallWS extends AsyncTask<String, String,String>{

                protected String doInBackground(String... params) {

                     SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
                     PropertyInfo pi =new PropertyInfo();
                     pi.setName("resaRef");
                     pi.setValue("400");
                     pi.setType(String.class);
                     request.addProperty(pi);

                     /*PropertyInfo pi =new PropertyInfo();
                     pi.setName("date");
                     pi.setValue("20130601");
                     pi.setType(String.class);
                     request.addProperty(pi);*/


                     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

                     envelope.dotNet = true;
                     envelope.setOutputSoapObject(request);
                     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

                     try{
                         androidHttpTransport.call(SOAP_ACTION, envelope);
                         final SoapObject response = (SoapObject)envelope.getResponse();
                         runOnUiThread(new Runnable() {

                             @Override
                             public void run() {
                                 ref.setText(response.getProperty("nom").toString());


                             }
                         });


                    }

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

                    }


                    return null;
                }



            }
        }

I have this Exception :

02-20 04:02:59.837: E/AndroidRuntime(1077): FATAL EXCEPTION: AsyncTask #1
02-20 04:02:59.837: E/AndroidRuntime(1077): Process: com.hotix.webser, PID: 1077
02-20 04:02:59.837: E/AndroidRuntime(1077): java.lang.RuntimeException: An error occured while executing doInBackground()
02-20 04:02:59.837: E/AndroidRuntime(1077):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.lang.Thread.run(Thread.java:841)
02-20 04:02:59.837: E/AndroidRuntime(1077): Caused by: java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
02-20 04:02:59.837: E/AndroidRuntime(1077):     at com.hotix.webser.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:48)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at com.hotix.webser.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
02-20 04:02:59.837: E/AndroidRuntime(1077):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
02-20 04:02:59.837: E/AndroidRuntime(1077):     ... 3 more

What's the problem ?

Was it helpful?

Solution 2

Solved :
I just created before showing the values of response in an EditText a SoapObject then

SoapObject obj=new SoapObject();
obj=(SoapObject) response.getProperty(0);

ref.setText(obj.getPrimitiveProperty("Nom").toString()+obj.getPrimitiveProperty("DateDepart").toString());

OTHER TIPS

I think this is related to adding the right library the correct way in Eclipse. This will most probably fix it:

  1. Check which JAR you are using for SOAP.
  2. Go to your Project -> Properties -> Java Build Path
  3. In the tab Libraries - Make sure you have added the JAR and you can see it.
  4. In the tab Order and Export - Check the box of Android Private Libraries and Android Dependencies (and maybe also the box of the JAR)

P.S. If it's still not working try the following:

  • Restart Eclipse and clean your project (Project>Clean)

  • Change the order of the items in "Order and Export". It may take a few tries before you get it right. The one below should work (what you have in your list may be different of course):

    1. src
    2. gen
    3. google APIs
    4. Android Private Libraries
    5. any library projects you have
    6. Android dependencies
    7. JARs
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top