2013-08-06 100 views
1

我試圖從Android活動訪問WSDL中定義的Web服務。預期的輸出是json格式的字符串。 在第一步中,我的目標是將此字符串打印到Android中的文本視圖。所以我用下面的代碼:當從Android訪問基於SOAP的Web服務時發生ClassCastException

import org.ksoap2.SoapEnvelope; 
import org.ksoap2.serialization.SoapObject; 
import org.ksoap2.serialization.SoapPrimitive; 
import org.ksoap2.serialization.SoapSerializationEnvelope; 
import org.ksoap2.transport.HttpTransportSE; 

import android.os.Bundle; 
import android.os.Handler; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.widget.TextView; 

public class MainActivity extends Activity { 
private final String NAMESPACE = "XXXXXXXXXXXXXXX"; 
private final String URL = "XXXXXXXXXXXXXXXXX"; 
private final String SOAP_ACTION = "XXXXXXXXXXXX"; 
private final String METHOD_NAME = "XXXXXXXXXXXXXXXXX"; 

private String webResponse = ""; 
private TextView textView; 
private Thread thread; 
private Handler handler = new Handler(); 

/** Called when the activity is first created. */ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    textView = (TextView) findViewById(R.id.textView1); 
    startWebAccess(); 
} 

public void startWebAccess() { 
    thread = new Thread() { 
     public void run() { 
      try { 
       SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME); 
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
         SoapEnvelope.VER11); 
       envelope.setOutputSoapObject(request); 
       HttpTransportSE androidHttpTransport = new HttpTransportSE(
         URL); 

       androidHttpTransport.call(SOAP_ACTION, envelope); 
       SoapPrimitive response = (SoapPrimitive) envelope 
         .getResponse(); 
       webResponse = response.toString(); 
       Log.d("TAG", webResponse); 
      } 

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

      handler.post(createUI); 
     } 
    }; 

    thread.start(); 
} 

final Runnable createUI = new Runnable() { 

    public void run() { 
     Log.d("TAG", webResponse); 
     textView.setText(webResponse); 
    } 
}; 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

但是,當我試圖運行的代碼,我可以看到在日誌貓以下錯誤(在文字沒有輸出):

java.lang.ClassCastException: java.util.Vector 

這裏是輸出完整日誌貓:

08-06 09:57:11.792: D/dalvikvm(953): GC_FOR_MALLOC freed 771 objects/322464 bytes in 130ms 
08-06 09:57:13.252: D/dalvikvm(953): GC_FOR_MALLOC freed 3182 objects/473904 bytes in 41ms 
08-06 09:57:13.252: D/NativeCrypto(953): Freeing OpenSSL session 
08-06 09:57:13.293: W/System.err(953): java.lang.ClassCastException: java.util.Vector 
08-06 09:57:13.293: W/System.err(953): at org.sahana.peoplelocator.MainActivity$2.run(MainActivity.java:56) 

什麼可以是錯誤?請幫助我我卡在這裏。

預先感謝您!

+0

是什麼線56 MainActivity.java? – Raghunandan

+0

請嘗試下面的內容並通知它是否不適合您。 – Raghunandan

+0

謝謝!解決了這個問題。 – Grant

回答

2

嘗試以下

替換此

 SoapPrimitive response = (SoapPrimitive) envelope 
        .getResponse(); 

通過

 SoapObject response=(SoapObject) envelope.bodyIn; 

我想你的代碼是這樣的結果我得到

enter image description here

+0

是的,你是正確的,很好地工作非常感謝你 – Grant

+1

@格蘭特歡迎你,並很高興它爲你工作。 – Raghunandan

1

當您嘗試轉換無效類型時,會引發ClassCastException。例如,假設您有一個ArrayList v。如果您執行String s=(String)v,它將拋出一個異常,因爲您無法將ArrayList轉換爲字符串。在這種情況下,您正在執行的唯一操作是SoapPrimitive response = (SoapPrimitive) envelope .getResponse();這意味着envelope.getResponse()不會返回SoapPrimitive.根據錯誤消息,它可能會返回java.util.Vector

我從來沒有使用任何你使用的庫,所以我不能給你一個明確的答案。但是,谷歌envelope.getResponse() ClassCastException給了我很多結果。試試看,祝你好運!

+0

這是錯誤。感謝您計算出來,順便說一下,圖書館是ksoap2 – Grant