如果声明其他声明失败

时间:2013-02-06 02:05:44

标签: java eclipse blackberry blackberry-eclipse-plugin

我有一个黑莓应用程序,基本上对我输入的信息进行选择查询,所以如果我输入“5”它会执行select * from table where id=5所以我得到结果。但是,该子句的else语句应返回“未找到结果”。代码发布如下:

public void onResponse(String data, int code) {

    final Vector vehicleInfo = jasonParser.parseVechicleResponse(data);

    if(vehicleInfo.size() > 0 ){
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                UiApplication.getUiApplication().pushScreen(new VehicleSearchResultScreen(etfVehicleInfo.getText(),vehicleInfo));
            }
        });
    }else{
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                Dialog.alert("No Results found.");
            }
        });
    }
}

此代码的问题是如果此vehicleinfo.size() = 0此else语句未运行。 运行的代码是下面发布的代码,这是我的错误代码。当收集错误发生时,应该调用它。所以基本上,当找不到记录时,它会显示连接错误的字符串,而我完全不知道为什么这个else语句没有运行;或者它是否运行,并且由以下声明过度运行?

public void onError(final String data, int code) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
            removeLoader();
            Dialog.alert("No Results Found!");
        }
    });
} 

解析代码

public Vector parseVehicleResponse(String response){
    Vector vector = new Vector();
    try{
        JSONObject jsonVehicleObject = new JSONObject(response);
        VehicleObject vehicle = new VehicleObject();
            vehicle.setChassisNum(jsonVehicleObject.getString("chassis_no"));//chassis_num
            vehicle.setEngineNum(jsonVehicleObject.getString("engine_no"));//engine_num
            vector.addElement(vehicle);
    }catch(Exception e){
        System.out.println("JSONResponseParser.parseNewsHeadlines() " + e );
    }

    return vector;
}

}

的Json

  // Return  encoded with JSON
            $result = array(

    "chassis_no"=>$chassis_no,
   "engine_no"=>$engine_no,

 );

2 个答案:

答案 0 :(得分:0)

在我看来,您的jasonParser.parseVechicleResponse(data);调用正在抛出错误,而您从未进入if语句。这可能是一个错字吗?你的意思是

parseVehicleResponse

而不是

parseVechicleResponse

(不是额外的“c”)??

答案 1 :(得分:0)

为了保持思想的流动,我在这里开始一个新的答案......您可以尝试通过添加“虚拟元素零”来修改parseVehicleResponse的代码,如下所示:

public Vector parseVehicleResponse(String response){
    Vector vector = new Vector();
    VehicleObject vehicle = new VehicleObject(); // create a "dummy object"
    vehicle.setChassisNum("-1"); // initialize members - with "valid" data... 
    vehicle.setEngineNum("-1");  // again - make it valid type, but otherwise "wrong"
    vector.addElement(vehicle);  // the vector now has at least one element

    // here comes your original code:
    try{
        JSONObject jsonVehicleObject = new JSONObject(response);
        VehicleObject vehicle = new VehicleObject();
            vehicle.setChassisNum(jsonVehicleObject.getString("chassis_no"));//chassis_num
            vehicle.setEngineNum(jsonVehicleObject.getString("engine_no"));//engine_num
            vector.addElement(vehicle);
    }catch(Exception e){
        System.out.println("JSONResponseParser.parseNewsHeadlines() " + e );
    }
    return vector;
}

现在,在主循环中,您可以执行以下操作:

Public void onResponse(String data, int code) {

    final Vector vehicleInfo = jasonParser.parseVechicleResponse(data);

    if(vehicleInfo.size() > 1 ){
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                // { add some code to strip the zero-th element from vehicleInfo() } 
                UiApplication.getUiApplication().pushScreen(new VehicleSearchResultScreen(etfVehicleInfo.getText(),vehicleInfo));
            }
        });
    }else{
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                removeLoader();
                Dialog.alert("No Results found.");
            }
        });
    }
}

这将缩小对问题的搜索范围......