试图将值从第一个java类传递到第二个java类

时间:2018-01-31 06:25:58

标签: java

这是我的第一个java类

    try {           

        //Build the query
        BasicDBObject query = new BasicDBObject();                                              
        query.put("building_Id", building_Id);
        query.put("floor_Id", floor_Id);

        DBObject removeIdProjection = new BasicDBObject("_id", 0);

        //Provide the query as an argument to the find()
        DBCursor cursor = col.find(query, removeIdProjection);          

        //Iterate the cursor
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        return new Status("success", cursor.toString());        

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

}

第一个java类的输出是

{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "512.9695434570312"}
{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "514.87548828125"}
{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "515.8284912109375"}
{"building_Id" : "3" , "floor_Id" : "23" , "x" : "717.5984497070312" , "y" : "517.7344360351562"}

我需要将上面的输出返回到第二类。

但是我只得到第二个java类的输出。

fetch paths response : {"code":"success","message":"DBCursor{collection=DBCollection{database=DB{name='trackbitDB'}, name='objects_path'}, find=FindOptions{, batchSize=0, limit=0, modifiers=null, projection=null, maxTimeMS=0, skip=0, sort=null, cursorType=NonTailable, noCursorTimeout=false, oplogReplay=false, partial=false}, cursor=null}"}

第二个java类

    try {

        // api path to list all paths based on floor and building  //fetchpath
        String uri = API_SERVER_PATH + "object/fetchpath?building_Id=" + building_Id + "&floor_Id=" + floor_Id;

        String result = ApiRequestHandler.sendGet(uri);
        System.out.println("fetch paths response : " + result);

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

    return null;
}

我觉得我在回来时犯了错误。有人帮我解决了这个问题。

提前致谢。

1 个答案:

答案 0 :(得分:-1)

返回新状态(“success”,cursor.toString());

你正在返回cursor.toString()这是游标的字符串表示,它不读取游标的内容,而是使用你在循环中使用的cursor.next()。

创建状态列表并将状态逐个添加到此列表并返回列表,请参阅以下代码:

try {           

    //Build the query
    BasicDBObject query = new BasicDBObject();                                              
    query.put("building_Id", building_Id);
    query.put("floor_Id", floor_Id);

    DBObject removeIdProjection = new BasicDBObject("_id", 0);

    //Provide the query as an argument to the find()
    DBCursor cursor = col.find(query, removeIdProjection);          
    List<Status> statusList = new ArrayList<>(); 
    //Iterate the cursor
    while (cursor.hasNext()) {
        Status status = new Status("success", cursor.next()); 
        statusList.add(status);
    }

    return statusList;

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