0
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;

}

Hi,

Value from the webservice is repeating when it is added to the arraylist.I clear the arraylist.But the issue still exixts.

My json response from webservice is: { "message": "success", "data": [ { "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 is repeating four times when added to arraylist Please help me?

Please suggest a method to solve this?

  • Please add the implementation of `VehicleDetails`. – JimmyB Apr 17 '14 at 07:17
  • Could it be that `VehicleDetails.vehicleNumber` is declared as `static`? – JimmyB Apr 17 '14 at 07:19
  • public class VehicleDetails { public static String vehicleNo=""; public static String vehicleId=""; } – user3544129 Apr 17 '14 at 08:10
  • vehicleNumber is declared as static in VehicleDetails – user3544129 Apr 17 '14 at 08:11
  • ... and that's your problem: Static fields only exist once, no matter how many instances you create via `new`. Remove those `static` declarations for the two fields and you should be good. - Besides, didn't your IDE/compiler warn you about the "non-static access to a static field" @ `vdetails.vehicleId = vehicleId` and `vdetails.vehicleNo = vehicleNumber`? – JimmyB Apr 17 '14 at 08:26
  • Maybe read up on static vs. non-static declarations again, [here](http://java.about.com/od/workingwithobjects/a/staticfields.htm) for instance. – JimmyB Apr 17 '14 at 08:32

1 Answers1

-1

your problem is that your recreating a single instance and because java is pass by reference all your values in arraylist point at the last instance.

to prevent this problem for each entry create a new instance;

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

vehicleArrayList.add(vdetails);

and delete this line

VehicleDetails vdetails = null;
EC84B4
  • 7,676
  • 4
  • 23
  • 34
  • 1
    "because java is pass by reference all your values in arraylist point at the last instance" is just not true. - And isn't what you're proposing exactly the code the OP already had? – JimmyB Apr 17 '14 at 09:03
  • as far as i know java is pass by reference and arryList only holds references of the entries, and he created one instance of VehicleDetails at the start of the method and in the for loop he just made that instance a new one its still the same reference so all items in arrayList point to a single instance which is the last item in json ! – EC84B4 Apr 17 '14 at 09:17
  • Maybe have a look at [this](http://stackoverflow.com/questions/40480/is-java-pass-by-reference). Also note that the `ArrayList` here stores references to *objects* (instances of `VehicleDetails`) and *not* references to references to objects. Thus, `vehicleArrayList.add(vdetails)` adds (=copies) a reference to the object referenced by `vdetails`, not a reference to `vdetails`. In C, `add` would be equivalently declared as `add( VehicleDetails* obj )`; what you're referring to would be `add( VehicleDetails** obj )` and is not what Java works like. – JimmyB Apr 17 '14 at 09:31
  • Plus, "for each entry create a new instance" is exactly what the OP was doing all the time. – JimmyB Apr 17 '14 at 09:32