Android - org.ksoap2.soapfault无法投射

问题描述 投票:4回答:3

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

“java.lang.classcastexception org.ksoap2.soapfault无法强制转换为org.ksoap2.serialization.soapobject”

你能帮助我吗?

这是我的客户端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;   
        }
    }
java android-ksoap2
3个回答
5
投票

这意味着这些参数找不到服务尝试此代码来查找错误消息:

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

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

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

并通过.wsdl或?wsdl结束Web服务的URL(同时尝试xD)

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

最后一个重要的事情是(当你使用android API时)通过IP更改localhost:

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

希望对你有所帮助!! ... 祝好运 !


4
投票

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

这种代码可以解决您的问题,

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
投票

与Web服务交互的最佳方式是在Web调试进程之前插入来自Web浏览器的数据并使用调试器进行检查。主要是在Web服务生成异常时发生。

© www.soinside.com 2019 - 2024. All rights reserved.