4

I'm trying to access a Java Web Service from Android client, but it is showing me an error:

"java.lang.classcastexception org.ksoap2.soapfault cannot be cast to org.ksoap2.serialization.soapobject"

Can you help me?

Here is my client web service code:

import java.lang.reflect.Method;

import android.app.Activity; 
import android.os.Bundle; 
import android.content.Context;
import android.content.Intent; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View;
import android.view.Window;
import android.widget.EditText;

import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;
import org.ksoap2.transport.HttpTransportSE; 

public class Loginuser extends Activity{


public static final int MENU1 = Menu.FIRST; 
public static final int MENU2 = Menu.FIRST + 1; 
public static final int MENU3 = Menu.FIRST + 2; 
public static Context group;

    private static final String SOAP_ACTION = "";
    private static final String METHOD_NAME = "logar";
    private static final String NAMESPACE = "http://wsproj.mycompany.com/";
    private static final String URL = "http://localhost:8084/wsproj/HelloWorld";


    EditText ura,pw; 

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.loginuser);

    }


    public void logar(View X) { 
    CarregaTelaBolarq();
    }

public void CarregaTelaBolarq(){

    ura=(EditText)findViewById(R.id.editText2);
    String raforn = ura.getText().toString();

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);


    request.addProperty("raforn",ura.getText().toString());

    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);


try{

    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    androidHttpTransport.call(SOAP_ACTION, envelope);

    SoapObject sp = (SoapObject)envelope.bodyIn;

    String result=sp.toString();

    if(result.equals("1"))

            {

               TextView tv; 
               tv=(TextView) findViewById(R.id.editText1);
               tv.setText("foi: ");
            }
            else
            {
                TextView tv; 
                tv=(TextView) findViewById(R.id.editText1);
                tv.setText("Msg from service: ");
            }       

        }
        catch(Exception e)
        {

            TextView tv=(TextView) findViewById(R.id.editText1);
            tv.setText("ERROR: " + e.toString());
        }

}




public boolean onCreateOptionsMenu(Menu options) { 
options.add(0, MENU1, 0, "Página Principal");
options.add(0, MENU2, 0, "Manual");
options.add(0, MENU3, 0, "Sobre");

return super.onCreateOptionsMenu(options);   }


public boolean onOptionsItemSelected(MenuItem item) {   
    switch (item.getItemId()) { 
    case MENU1: 
        Intent mudarHome= new Intent(this, MainActivity.class); 
        startActivity(mudarHome);  
        return true;

    case MENU2: 
        Intent mudarManual = new Intent(this, Manual.class); 
        startActivity(mudarManual); 
        return true;

    case MENU3: 
        Intent mudarSobre = new Intent(this, Sobre.class); 
        startActivity(mudarSobre);  
        return true;

        }   
        return false;   
        }
    }
2
  • At what line is the exception happening? You can find this out by running your app in the debugger and looking at the exception details.
    – Dan Hulme
    Oct 28, 2012 at 23:00
  • When I try to run debugger when app opens shows me "Waiting for debugger - Force Close".
    – JulToldo
    Oct 28, 2012 at 23:11

3 Answers 3

5

That's means there is no service found by those parameters try this code to find the error message :

SoapFault error = (SoapFault)envelope.bodyIn;
System.out.println("Error message : "+error.toString());

in my opinion you must fill the SOAP_ACTION parameter by the class that include the service with the package name :

private static final String SOAP_ACTION = "http://com.mycompany.wsproj/HelloWorld";

and end the URL of the web service by .wsdl or ?wsdl ( try them both xD )

private static final String URL = "http://localhost:8084/wsproj/HelloWorld?wsdl";

one last important thing is (when you are using android API ) change the localhost by the IP :

private static final String URL = "http://10.0.2.2:8084/wsproj/HelloWorld?wsdl";

Hope that helps you !! ... good luck !

0
4

when you are dealing with SOAP Web Service, This problem may come some time. The response coming from the service can either be a SOAP Object and if something goes wrong like wrong credentials passed then Response comes with error message and it's a SOAPFAULT Object. So update your code of parsing to check the type of the response object.

This sort of code can solve your problem,

if (envelope.bodyIn instanceof SoapFault) {
    String str= ((SoapFault) envelope.bodyIn).faultstring;
    Log.i("", str);

    // Another way to travers through the SoapFault object
/*  Node detailsString =str= ((SoapFault) envelope.bodyIn).detail; 
    Element detailElem = (Element) details.getElement(0) 
                 .getChild(0); 
    Element e = (Element) detailElem.getChild(2);faultstring; 
    Log.i("", e.getName() + " " + e.getText(0)str); */
} else {
    SoapObject resultsRequestSOAP = (SoapObject) envelope.bodyIn;
    Log.d("WS", String.valueOf(resultsRequestSOAP));
}
0
0

The best way to interact with the web services just insert the data from web browser and check it with debugger before the android debugging process. Mostly it occurs when the web service generates an exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.