2013-03-26 57 views
1

Heyho。我正在寫一個Android客戶端,它在Hessian ServletHessdroid之間進行通信。一切都很好,直到我想將本selfimplemented類型:Android上的與Hessian Servlet進行數據通信後的ClassCastException

  • 客戶&服務器都IUserService.java接口之間的通信。

服務器端:

odtObject.java:

import java.io.Serializable; 

public class odtObject implements Serializable { 

    private String siteName; 
    private int siteId; 

    public odtObject(String siteName, int siteId) { 
     this.siteName = siteName; 
     this.siteId = siteId; 
    } 

    public String getSiteName() { 
     return siteName; 
    } 

    public int getSiteId() { 
     return siteId; 
    } 
} 

UserService.java:

public ArrayList<odtObject> getSiteList() { 
    ArrayList<odtObject> siteList = new ArrayList<odtObject>(); 
    siteList.add(new odtObject("hello", 1)); 
    System.out.println(siteList.get(0).getSiteName()); // out: hello 
    return siteList; 
} 

客戶端

PageOne.java:

IUserService con = new IUserService(); 
ArrayList<odtObject> siteList = new ArrayList<odtObject>(); 
siteList = con.getSiteList(); 
System.out.println(siteList.get(0).getSiteName()); // 87: Exception 

odtObject.java:...

例外:

FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp/com.testapp.PageOne}: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.testapp.odtObject 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
    at android.app.ActivityThread.access$600(ActivityThread.java:141) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5041) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.ClassCastException: java.util.HashMap cannot be cast to com.testapp.odtObject 
    at com.testapp.PageOne.onCreate(PageOne.java:87) 
    at android.app.Activity.performCreate(Activity.java:5104) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 

謝謝你的幫忙!只是說,如果你想看到更多的代碼...

編輯:

RetrieveData調用IUserService功能... PAGEONE代碼上面是一種僞代碼來了解它更容易。

protected void onCreate(Bundle savedInstanceState) {   
    Thread t = new Thread() { 
     public void run() { 
      Intent myIntent = getIntent(); 
      String user = myIntent.getStringExtra("user"); 
      String pass = myIntent.getStringExtra("pass"); 
      RetrieveData r = new RetrieveData(context);    
      if(r.login(user, pass.toCharArray()) != null) {     
       siteList = r.getSiteList(); 
       System.out.println("done"); 
      } else { 
       System.out.println("error"); 
      } 

     } 
    };  
    t.start();     
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page1); 

    inputSearch = (EditText) findViewById(R.id.inputSearch); 
    listView = (ListView) findViewById(R.id.mylist); 

    /* wait for networking thread */ 
    try { 
     t.join(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    }   
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1);  

    System.out.println(siteList.get(0).getSiteName()); // 87: Exception  
    for (odtObject d : siteList) { 
     adapter.add(d.getSiteName()); 
    } 
    listView.setAdapter(adapter); 

    ... 
} 

也許這可能是相關的也:

public String login(String username, char[] password) { 
    HessianProxyFactory factory = new HessianProxyFactory();   
    String url = "http://192.168.56.1:8080/hessianServerX"; 
    factory.setHessian2Reply(false); // avoid hessian reply error 
    try { 
     userCon = (IUserService) factory.create(IUserService.class, 
       url+"/IUserService");     
     sessionId = userCon.login(username, password); 
     secureCon = (ISecureService) factory.create(ISecureService.class, 
       url+"/restrictedArea/ISecureService;jsessionid="+sessionId);   
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 
    return sessionId; 
} 

編輯:

我設置DebugMode對HessianProxyFactory,現在我得到這樣一個:

03-26 16:07:06.909: W/SerializerFactory(6023): Hessian/Burlap: 'com.hessian.odtObject' is an unknown class in dalvik.system.PathClassLoader[dexPath=/data/app/tsch.serviceApp-1.apk,libraryPath=/data/app-lib/tsch.serviceApp-1]: 
03-26 16:07:06.909: W/SerializerFactory(6023): java.lang.ClassNotFoundException: com.hessian.odtObject 
03-26 16:07:06.909: W/dalvikvm(6023): threadid=10: thread exiting with uncaught exception (group=0x9e50d908) 
03-26 16:07:06.909: E/AndroidRuntime(6023): FATAL EXCEPTION: Thread-527 
03-26 16:07:06.909: E/AndroidRuntime(6023): java.lang.ClassCastException: java.util.HashMap cannot be cast to tsch.serviceApp.types.odtObject 
03-26 16:07:06.909: E/AndroidRuntime(6023):  at tsch.serviceApp.net.HessianConnect.<init>(HessianConnect.java:31) 
03-26 16:07:06.909: E/AndroidRuntime(6023):  at tsch.serviceApp.PageLogin$1.run(PageLogin.java:17) 
+1

是「at com.testapp.PageOne.onCreate(PageOne.java:87)」System.out.println(siteList.get(0))。getSiteName()); ? – Blackbelt 2013-03-26 11:48:01

+0

是的。抱歉。 – trek711 2013-03-26 11:49:24

+0

它對我沒有意義。你可以發佈整個onCreate嗎? – Blackbelt 2013-03-26 11:50:20

回答

1

自己解決了這個問題。我必須將自定義類型類OdtObject.java作爲jar導出並添加到類路徑中!

0

如果此LOP的結果爲false:

if(r.login(user, pass.toCharArray()) != null) {     
       siteList = r.getSiteList(); 
       System.out.println("done"); 
      } else { 
       System.out.println("error"); 
      } 

然後您的siteList將會爲空並且未初始化。

+0

是的,你是對的!但它不是空的! – trek711 2013-03-26 12:26:54

相關問題