-1

我有一个黑莓应用程序,它基本上对我输入的信息进行选择查询,所以如果我输入“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,

 );
4

2 回答 2

0

在我看来,您的jasonParser.parseVechicleResponse(data);电话正在引发错误,并且您永远不会得到该if声明。会不会是笔误?你的意思

parseVehicleResponse

代替

parseVechicleResponse

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

于 2013-02-06T02:09:23.963 回答
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.");
            }
        });
    }
}

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

于 2013-02-07T02:37:57.547 回答