0

I'm trying soap parsing, but it doesn't work. In my application I'm using 1 edittext, 1 button, 1 textview,after ran the application, it will show output window. but after I entered any value in edittext and click the button, it will come error. I dono how to solve, will yu pls anyone help me, My MainActivity is below..

public class MainActivity extends Activity {    
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://www.w3schools.com/webservices/tempconvert.asmx";
private final String SOAP_ACTION = "http://tempuri.org/CelsiusToFahrenheit";
private final String METHOD_NAME = "CelsiusToFahrenheit";
private String TAG = "Sakthi";
private static String celcius;
private static String fahren;
Button b;
TextView tv;
EditText et;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     //Celcius Edit Control
    et = (EditText) findViewById(R.id.editText1);
    //Fahrenheit Text control
    tv = (TextView) findViewById(R.id.tv_result);
    //Button to trigger web service invocation
    b = (Button) findViewById(R.id.button1);
    //Button Click Listener
    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            //Check if Celcius text control is not empty
            if (et.getText().length() != 0 && et.getText().toString() != "") {
                //Get the text control value
                celcius = et.getText().toString();
                //Create instance for AsyncCallWS
                AsyncCallWS task = new AsyncCallWS();
                //Call execute 
                task.execute();
            //If text control is empty
            } else {
                tv.setText("Please enter Celcius"); 
            }
        }
    });
}
public class AsyncCallWS extends AsyncTask<String, Void, integer> {
    @Override
    protected integer doInBackground(String... params) {
        Log.i(TAG, "doInBackground");
        getFahrenheit(celcius);
        return null;
    }

    public void getFahrenheit(String celsius) {
        //Create request
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
        //Property which holds input parameters
        PropertyInfo celsiusPI = new PropertyInfo();
        //Set Name
        celsiusPI.setName("Celsius");
        //Set Value
        celsiusPI.setValue(celsius);
        //Set dataType
        celsiusPI.setType(double.class);
        //Add the property to request object
        request.addProperty(celsiusPI);
        //Create envelope
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;
        //Set output SOAP object
        envelope.setOutputSoapObject(request);
        //Create HTTP call object
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        try {
            //Invole web service
            androidHttpTransport.call(SOAP_ACTION, envelope);
            //Get the response
            SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
            //Assign it to fahren static variable
            fahren = response.toString();

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

    @Override
    protected void onPostExecute(integer result) {
        Log.i(TAG, "onPostExecute");
        tv.setText(fahren + "° F");
    } 

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
        tv.setText("Calculating...");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}'

error in logcat is like this

'**06-19 07:20:22.613: E/AndroidRuntime(1525): FATAL EXCEPTION: AsyncTask #1
06-19 07:20:22.613: E/AndroidRuntime(1525): java.lang.RuntimeException: An error occured while executing doInBackground()
06-19 07:20:22.613: E/AndroidRuntime(1525):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.lang.Thread.run(Thread.java:856)
06-19 07:20:22.613: E/AndroidRuntime(1525): Caused by: java.lang.NoClassDefFoundError: org.ksoap2.serialization.SoapObject
06-19 07:20:22.613: E/AndroidRuntime(1525):     at com.example.medimixsoap.MainActivity$AsyncCallWS.getFahrenheit(MainActivity.java:74)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at com.example.medimixsoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:68)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at com.example.medimixsoap.MainActivity$AsyncCallWS.doInBackground(MainActivity.java:1)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-19 07:20:22.613: E/AndroidRuntime(1525):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
06-19 07:20:22.613: E/AndroidRuntime(1525):     ... 4 more**'
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
It's Me SMA
  • 71
  • 2
  • 9
  • clean the project and run it again, it has noclassdefinition found error. it is not able to find .class file at run time – abhi Jun 19 '13 at 07:41
  • 1
    celsiusPI.setType(double.class); probably you meant to write celsiusPI.setType(Double.class); – Blackbelt Jun 19 '13 at 07:42
  • try this link http://stackoverflow.com/questions/17020176/java-lang-noclassdeffounderror-com-applovin-sdk-applovinsdk/17020252#17020252 – Sunil Kumar Jun 19 '13 at 07:52
  • check this might help http://stackoverflow.com/questions/15521740/to-use-the-tutorial-in-android-4-0-3-if-had-to-work-with-asynxtasc-but-i-still-d/15521935#15521935 – Raghunandan Jun 19 '13 at 08:02

2 Answers2

0

Do you use external library for Soap objects? Check that your library included in final apk: Project properties -> Java Build Path -> Tab Order and Export and check that "Android Private Libraries" is checked.

Dimmerg
  • 2,113
  • 1
  • 13
  • 14
0

Instead of:

AsyncCallWS task = new AsyncCallWS();
//Call execute 
task.execute();

Try putting this:

AsyncCallWS threadsStart = new AsyncCallWS();
threadsStart.execute(celcius);

try {
  farenheit = threadsStart.get();
} catch (InterruptedException e) {
  e.printStackTrace();
} catch (ExecutionException e) {
  e.printStackTrace();
}

Then in the AsyncCallWS class instead of:

@Override
protected integer doInBackground(String... params) {
    Log.i(TAG, "doInBackground");
    getFahrenheit(celcius);
    return null;
}

Try putting this:

@Override
protected integer doInBackground(String... params) {
    Log.i(TAG, "doInBackground");
    getFahrenheit(params);
    return null;
}

The problem I think comes from the fact that you're not waiting for the background task to finish.

g00dy
  • 6,752
  • 2
  • 30
  • 43