将web服务添加到arraylist时,最后一个值会重复

时间:2014-04-17 07:02:23

标签: java android web-services arraylist

public static VehicleDetails[] getAllVehicles(String clientCode,String secretCode) throws ClientProtocolException,
    IOException, JSONException {
        VehicleDetails[] vd = null;
String result = null;

VehicleDetails vdetails = null;
ArrayList<VehicleDetails> vehicleArrayList = new ArrayList<VehicleDetails>();
JSONObject jObject = null;
String loginUrl = "getAllVehicles";

try {

    HttpPost request = new HttpPost(URL + loginUrl);
    List<NameValuePair> postParameters = new ArrayList<NameValuePair>();

    postParameters.add(new BasicNameValuePair("clientCode", clientCode));
    postParameters.add(new BasicNameValuePair("secretCode",  secretCode));


    UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
            postParameters);
    request.setEntity(entity);
    HttpResponse response = getThreadSafeClient().execute(request);
    entityResponse = response.getEntity();
    result = EntityUtils.toString(entityResponse, HTTP.UTF_8);
     Log.d(TAG, "result>>" + result);
    JSONObject object = (JSONObject) new JSONTokener(result)
            .nextValue();
    VehicleDetails.status_login = object.getString("message");

    if (VehicleDetails.status_login.contentEquals("success")) {
        JSONArray array = object.getJSONArray("data");
        vehicleArrayList.clear();
        for (int i = 0; i < array.length(); i++) {
            Log.d(TAG, "LiveTracking>>>>>>>>>>>");


            JSONObject jObj = array.getJSONObject(i);
            String vehicleId = jObj.getString("vehicle_id").toString();
            String vehicleNumber = jObj.getString("vehicle_number").toString();


            vdetails = new VehicleDetails();

            vdetails.vehicleId = vehicleId;
            vdetails.vehicleNo = vehicleNumber;

            vehicleArrayList.add(vdetails);

        }
        vd = new VehicleDetails[vehicleArrayList.size()];

        for (int x = 0; x < vehicleArrayList.size(); ++x) {

            vd[x] = (VehicleDetails) vehicleArrayList.get(x);
        }
    } else if(VehicleDetails.status_login.contentEquals("failed")){

        JSONArray array = object.getJSONArray("data");

        for (int i = 0; i < array.length(); i++) {
            JSONObject jObj = array.getJSONObject(i);

            vdetails.failReason = jObj.getString("data").toString();



        }

    }

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

return vd;

}

您好,

web服务的值在重新添加到arraylist时会重复。我清除了arraylist。但问题仍然存在。

来自webservice的我的json回复是: {     “消息”:“成功”,     “数据”:[         {             “vehicle_id”:“7”,             “vehicle_number”:“KL-01 BA 2233”         },         {             “vehicle_id”:“2”,             “vehicle_number”:“KL 01 AP 9650”         },         {             “vehicle_id”:“10”,             “vehicle_number”:“KL 01 AP 9650 N”         },         {             “vehicle_id”:“9”,             “vehicle_number”:“HB”         }     ] }

HB添加到arraylist时重复四次 请帮帮我?

请建议一种方法来解决这个问题?

1 个答案:

答案 0 :(得分:-1)

你的问题是你重新创建一个单独的实例,因为java通过引用传递了最后一个实例的arraylist中的所有值。

为每个条目防止此问题创建一个新实例;

VehicleDetails vdetails = new VehicleDetails();
vdetails.vehicleId = vehicleId;
vdetails.vehicleNo = vehicleNumber;

vehicleArrayList.add(vdetails);

并删除此行

VehicleDetails vdetails = null;