Jena Sparql错误java.lang.Integer

时间:2014-01-29 15:01:48

标签: java sparql jena

我有以下代码:

    RestClient client = new RestClient("http://localhost:8080/scheduler.core/rest/services/discoverSensors");

    client.AddParam("userID", "keith@acrosslimits.com");
    client.AddParam("longitude", "4.3512725830078125");
    client.AddParam("latitude", "50.84761359783461");
    client.AddParam("radius", "15.0");
    client.AddHeader("accept", "application/xml");

    try {
        client.Execute(RestClient.RequestMethod.GET);
    } catch (Exception e) {
        e.printStackTrace();
    }

    String response = client.getResponse();
    //System.out.println(response);

    DocumentBuilder db = null;
    try {
        db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block

        System.out.println("error");
    }

    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(response));

    Document doc = db.parse(is);
    NodeList nodes = doc.getElementsByTagName("SensorType");

    // Getting the id of the sensor
    for(int x=0,size= nodes.getLength(); x<size; x++) {

        String resourceID = nodes.item(x).getAttributes().getNamedItem("id").getNodeValue();

        String sparqlQueryString = "select ?sensor ?lat ?lng "+
                                   "from <http://lsm.deri.ie/OpenIoT/demo/sensormeta#> " +
                                   "where{?sensor <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://purl.oclc.org/NET/ssnx/ssn#Sensor>."+
                                   "?sensor <http://lsm.deri.ie/ont/lsm.owl#hasSensorType> <http://lsm.deri.ie/resource/8a8291b73215690e013215a4c0d00f17>."+
                                   "?sensor <http://www.loa-cnr.it/ontologies/DUL.owl#hasLocation> ?location."+
                                   "?location <http://www.w3.org/2003/01/geo/wgs84_pos#lat> ?lat."+
                                   "?location <http://www.w3.org/2003/01/geo/wgs84_pos#long> ?lng."+
                                   "}limit 100";

        System.out.println(resourceID);
        System.out.println(sparqlQueryString);

        Query query = QueryFactory.create(sparqlQueryString);
        ARQ.getContext().setTrue(ARQ.useSAX);

        System.out.println("1");
        QueryExecution qexec = QueryExecutionFactory.sparqlService("http://lsm.deri.ie/sparql", query);
        System.out.println("2");

        //Retrieving the SPARQL Query results
        ResultSet results = qexec.execSelect();
        System.out.println("3");

        //Iterating over the SPARQL Query results
        while (results.hasNext()) {
             QuerySolution soln = results.nextSolution();

             System.out.println("4");
             String SensorID = soln.get("?sensor").toString();
             String lat = soln.get("?lat").toString();
             String lon = soln.get("?lng").toString();

             System.out.println(SensorID);
             System.out.println(lat);
             System.out.println(lon);
        }

        qexec.close();
    }
}

我面临的问题是,当我执行查询时,会显示错误。程序终止的行是2到3之间的行。显示的错误是:

Exception in thread "main" java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer
    at org.apache.http.params.AbstractHttpParams.getIntParameter(AbstractHttpParams.java:70)
    at org.apache.http.client.params.HttpClientParamConfig.getRequestConfig(HttpClientParamConfig.java:54)
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:806)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.apache.http.impl.client.DecompressingHttpClient.execute(DecompressingHttpClient.java:158)
    at org.apache.http.impl.client.DecompressingHttpClient.execute(DecompressingHttpClient.java:139)
    at org.apache.jena.riot.web.HttpOp.exec(HttpOp.java:1011)
    at org.apache.jena.riot.web.HttpOp.execHttpGet(HttpOp.java:291)
    at org.apache.jena.riot.web.HttpOp.execHttpGet(HttpOp.java:353)
    at com.hp.hpl.jena.sparql.engine.http.HttpQuery.execGet(HttpQuery.java:326)
    at com.hp.hpl.jena.sparql.engine.http.HttpQuery.exec(HttpQuery.java:276)
    at com.hp.hpl.jena.sparql.engine.http.QueryEngineHTTP.execSelect(QueryEngineHTTP.java:346)
    at ExecuteRestClient.main(ExecuteRestClient.java:114)

这是因为纬度和经度变量?我正在使用jena 2.11。

2 个答案:

答案 0 :(得分:1)

@ZaoTaoBao在评论中指出的可能是您问题的根源 - https://github.com/pyvandenbussche/sparqles/issues/9

ARQ当前没有使用最新版本的Apache HTTP Client,但是你的应用程序/类路径中的东西是错误的类(从ARQ的角度来看)正在加载。因此,当ARQ尝试使用HttpClient API时,它期望它最终调用新API中的方法,这些方法不再具有相同的类型签名,并且您获得ClassCastException

修改

因此,这归结为Java / app特定的环境问题。

您需要检查您的类路径和/或依赖关系层次结构,以确定使用Apache HttpClient的其他内容,并通过不使用比ARQ更新的版本来解决版本冲突。

mvn dependency:tree是您在maven项目上调查此问题的朋友,然后您可以使用依赖项排除删除较新版本。否则,您将需要使用IDE或构建系统提供的任何依赖/类路径管理支持来调查此问题。

但是,如果您要混合需要多个HttpClient版本的库,那么这样做只会破坏别的东西。

答案 1 :(得分:-1)

Long和Integer是两个不同的类。

看看这个:

Integer Long

因此,您可以“轻松地”将Integer转换为Long,但是Long to Integer您必须控制异常,或者是否可能。

将对象强制转换为另一个对象,可以使用以下静态方法: Integer.valueOf(X)或Long.valueOf(X)

我希望它有所帮助。