java Android - org.ksoap2.soapfault 无法转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13113733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:32:08  来源:igfitidea点击:

Android - org.ksoap2.soapfault cannot be cast

javaandroid-ksoap2

提问by JulToldo

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

我正在尝试从 Android 客户端访问 Java Web 服务,但它显示了一个错误:

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

“java.lang.classcastexception org.ksoap2.soapfault 不能转换为 org.ksoap2.serialization.soapobject”

Can you help me?

你能帮助我吗?

Here is my client web service code:

这是我的客户端 Web 服务代码:

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;   
        }
    }

回答by Shessuky

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 :

在我看来,您必须通过包含具有包名称的服务的类来填充 SOAP_ACTION 参数:

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 )

并通过 .wsdl 或 ?wsdl 结束 Web 服务的 URL(尝试它们两个 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 :

最后一件重要的事情是(当您使用 android API 时)通过 IP 更改本地主机:

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

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

希望能帮到你!!... 祝你好运 !

回答by Vivek Bansal

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.

当您处理 SOAP Web Service 时,这个问题可能会出现一段时间。来自服务的响应可以是一个 SOAP 对象,如果出现错误,比如传递了错误的凭据,那么响应会带有错误消息,它是一个 SOAPFAULT 对象。因此,更新您的解析代码以检查响应对象的类型。

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

回答by Sajid khan

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.

与 Web 服务交互的最佳方式是从 Web 浏览器插入数据,并在 android 调试过程之前用调试器检查它。大多数情况下,它发生在 Web 服务生成异常时。