2

我在 Tomcat 服务器上将此代码作为 Java 应用程序运行。

if(levelMap.get(bssid_a) == null)
    continue;
ArrayList<String> levels_a = new ArrayList<String>();
levels_a = (ArrayList<String>)levelMap.get(bssid_a);

其中levelMap是一个hashmap,定义为:

HashMap<String, ArrayList<String>> levelMap

我收到此错误:

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.ArrayList

相同的代码在我的 Android 应用程序中运行,所以我不确定为什么会收到此错误。

原始函数(没有类型转换,但给了我同样的错误):

    public EDistance[] getLocation(HashMap<String, ArrayList<String>> levelMap) {

        HashMap<String, CrossProduct> crossProductList_local = new HashMap<String, CrossProduct>();

        LinkedHashSet<String> localList = (LinkedHashSet<String>) globalList.clone();

        Iterator<String> cpItera = localList.iterator();
        long time1 = System.currentTimeMillis();
        // Iterator<String> levelIter = levelMap.keySet().iterator();
        // try {

        while (cpItera.hasNext()) {

            String ssids[] = cpItera.next().split(",");
            String bssid_a = ssids[0];

            if(levelMap.get(bssid_a) == null)
                continue;
            //ArrayList<String> levels_a = new ArrayList<String>();
            ArrayList<String> levels_a = levelMap.get(bssid_a);
            double meanA = mean(levels_a, sampleSize, defaultLevel);

            // Iterator<String> cpIterb = localList.iterator();
            //
            // while(cpIterb.hasNext())
            // {

            String bssid_b = ssids[1];
            // if(bssid_a.equals(bssid_b))
            // continue;
            if(levelMap.get(bssid_b) == null)
                continue;
            //ArrayList<String> levels_b = new ArrayList<String>();
            ArrayList<String> levels_b = levelMap.get(bssid_b);
            double meanB = mean(levels_b, sampleSize, defaultLevel);
............

这是堆栈跟踪:

SEVERE: Failed to invoke method distance in class com.fingerprint.core.Calculate: [Ljava.lang.Object; cannot be cast to java.util.ArrayList
org.apache.xmlrpc.common.XmlRpcInvocationException: Failed to invoke method distance in class com.fingerprint.core.Calculate: [Ljava.lang.Object; cannot be cast to java.util.ArrayList
    at org.apache.xmlrpc.server.ReflectiveXmlRpcHandler.invoke(ReflectiveXmlRpcHandler.java:129)
    at org.apache.xmlrpc.server.ReflectiveXmlRpcHandler.execute(ReflectiveXmlRpcHandler.java:106)
    at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:46)
    at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:86)
    at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:200)
    at org.apache.xmlrpc.webserver.XmlRpcServletServer.execute(XmlRpcServletServer.java:112)
    at org.apache.xmlrpc.webserver.XmlRpcServlet.doPost(XmlRpcServlet.java:196)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.ArrayList
    at com.fingerprint.location.HyperbolicEuclideanDistance.getLocation(HyperbolicEuclideanDistance.java:235)
    at com.fingerprint.core.Calculate.distance(Calculate.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.xmlrpc.server.ReflectiveXmlRpcHandler.invoke(ReflectiveXmlRpcHandler.java:115)
    ... 24 more
Caused by:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.ArrayList
    at com.fingerprint.location.HyperbolicEuclideanDistance.getLocation(HyperbolicEuclideanDistance.java:235)
    at com.fingerprint.core.Calculate.distance(Calculate.java:30)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.xmlrpc.server.ReflectiveXmlRpcHandler.invoke(ReflectiveXmlRpcHandler.java:115)
    at org.apache.xmlrpc.server.ReflectiveXmlRpcHandler.execute(ReflectiveXmlRpcHandler.java:106)
    at org.apache.xmlrpc.server.XmlRpcServerWorker.execute(XmlRpcServerWorker.java:46)
    at org.apache.xmlrpc.server.XmlRpcServer.execute(XmlRpcServer.java:86)
    at org.apache.xmlrpc.server.XmlRpcStreamServer.execute(XmlRpcStreamServer.java:200)
    at org.apache.xmlrpc.webserver.XmlRpcServletServer.execute(XmlRpcServletServer.java:112)
    at org.apache.xmlrpc.webserver.XmlRpcServlet.doPost(XmlRpcServlet.java:196)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

调用 XMlRPC 方法的代码:

public String getLocationXMLRPC(HashMap<String, ArrayList<String>> levelMap, String mapFile) {

    try {
        XMLRPCClient client = new XMLRPCClient("http://172.28.184.222:8080/LocationService/xmlrpc");

        Object[] params = new Object[]{levelMap, mapFile};
        //EDistance retura[] = (EDistance[]) client.callEx("Calculate.distance", params);
        String result = (String) client.callEx("Calculate.distance", params);
        return result;

    } catch (XMLRPCException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

调用错误函数的 XMLRPC 方法:

public String distance(HashMap<String, ArrayList<String>> levelMap, String mapFile)
{
    if(dist == null){
        dist = new HyperbolicEuclideanDistance(mapFile, 3, -130,
                false, "fake.txt");
        System.out.println("New Class created!");
    }
    EDistance[] result =  dist.getLocation(levelMap);

    StringBuilder build = new StringBuilder();

    for (int j = 0; j < result.length; j++) {
        if (result[j] != null)
            build.append(j + ") " + result[j] + "\n");
    }
    return build.toString(); 

}

调用服务器的代码:

public String getLocationXMLRPC(HashMap<String, ArrayList<String>> levelMap, String mapFile) {

    try {
        XMLRPCClient client = new XMLRPCClient("http://172.28.184.222:8080/LocationService/xmlrpc");


        String result = (String) client.call("Calculate.distance", levelMap, mapFile);
        return result;

    } catch (XMLRPCException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
4

2 回答 2

2

根据ws-xmlrpc XMLRPC Data-Types aHashMap<String, ArrayList<String>>不是直接支持的参数类型。

从您发布的异常堆栈跟踪中可以看到,HashMap<String, Object>它沿着线路连接到您的 XMLRPC 服务器。这很明显,因为服务器需要distance使用 Java 反射调用该方法。这样做,如果参数至少不是 HashMap,则会发生异常。相反,当从该映射中检索对象时,跟踪中的异常会发生。从 检索和分配元素时,在运行时会进行不可见的类型检查HashMap<String, ArrayList<String>>,并且该检查会失败,并出现异常。

因此,XMLRPC 库传递参数可能是一个编组问题。因此,Map<String, List<String>>根据上述文档应该是一个有效的参数类型,尝试将distance方法及其被调用者参数更改为该参数类型,然后重试。

如果这也失败了,请考虑以另一种更扁平和简单的结构传递参数。

于 2012-07-14T14:34:30.980 回答
0

我自己也遇到了类似的问题。似乎 ws-xmlrpc 将另一个结构中的 List 转换为数组,即使函数参数被指定为 List。因此 Map[String,List[String]] 被转换为 Map[String,Object] ,其中 Object 是一个字符串数组。我通过将传入的列表作为数组处理来解决这个问题。在某些情况下,我使用 instanceof 来区分数组和列表。

于 2013-01-09T13:47:04.297 回答