2012-10-28 25 views
3

我想從Android客戶端訪問Java Web服務,但它顯示了我的錯誤:的Android - org.ksoap2.soapfault無法施展

「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; 
     } 
    } 
+0

在什麼行是異常發生?您可以通過在調試器中運行您的應用程序並查看異常詳細信息來找到它。 –

+0

當我嘗試運行調試器時,應用程序打開顯示我「等待調試器 - 強制關閉」。 – JulToldo

回答

4

這意味着沒有通過這些參數發現服務嘗試這個代碼,找出錯誤消息:

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(都去嘗試一下XD)

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

最後一個重要的事情是結束Web服務的URL(當您使用的Android API)由IP改變本地主機:

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

希望能幫到你! ... 祝你好運 !

+0

謝謝你的回覆也幫助了我;-) –

4

當您處理SOAP Web服務時,可能會出現此問題一段時間。來自該服務的響應可以是SOAP對象,如果出現錯誤,如錯誤憑據傳遞,則Response將帶有錯誤消息,並且它是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

謝謝:-)幫了我很多。 – user1007522

相關問題