Heyho。我正在编写一个在Hessian Servlet和Hessdroid之间进行通信的Android客户端。一切都很顺利,直到我想传输这个自我实现的类型:
服务器端:
的 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;
}
编辑:
我在HessianProxyFactory上设置了DebugMode,现在我得到了这个:
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)
答案 0 :(得分:1)
自己解决问题。我必须将自定义类型类OdtObject.java导出为jar并将其添加到类路径中!
答案 1 :(得分:0)
如果此lop的结果为false:
if(r.login(user, pass.toCharArray()) != null) {
siteList = r.getSiteList();
System.out.println("done");
} else {
System.out.println("error");
}
然后您的siteList
将为空且未初始化。