来自webservice的价值正在重复

时间:2014-04-16 06:17:06

标签: android web-services arraylist

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

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

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));
    postParameters.add(new BasicNameValuePair("vehicleNumber", vehicleId));

    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");
        for (int i = 0; i < array.length(); i++) {
            JSONObject jObj = array.getJSONObject(i);
            String driver = jObj.getString("driver").toString();
            String speed = jObj.getString("speed").toString();
            String longitude=jObj.getString("longitude").toString();
            String last_updated_time=jObj.getString("last_updated_time").toString();
            String  gps_status=jObj.getString("gps_status").toString();
            String driver_mobile_no=jObj.getString("driver_mobile_no").toString();
            String vehicle_idle_status=jObj.getString("vehicle_idle_status").toString();
            String latitude=jObj.getString("latitude").toString();
            String ac_status=jObj.getString("ac_status").toString();
            String device_mobile_no=jObj.getString("device_mobile_no").toString();
            String engine_status=jObj.getString("engine_status").toString();

            vdetails = new VehicleDetails();

            vdetails.driverName = driver;
            vdetails.vehicleSpeed = speed;
            vdetails.latitude=latitude;
            vdetails.longitude=longitude;
            vdetails.lastUpdatedDateTime=last_updated_time;
            vdetails.gpsStatus=gps_status;
            vdetails.driverMobNo=driver_mobile_no;
            vdetails.acStatus=ac_status;
            vdetails.engineStatus=engine_status;
            vdetails.vehicleIdleStatus=vehicle_idle_status;
            vdetails.deviceMobNo=device_mobile_no;
            LiveTrackArrayList.add(vdetails);

        }
        vd = new VehicleDetails[LiveTrackArrayList.size()];
        for (int x = 0; x < LiveTrackArrayList.size(); ++x) {                   
            vd[x] = (VehicleDetails) LiveTrackArrayList.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;

}

您好, 我正在开发一个车辆跟踪应用程序。对于实时跟踪,我们使用webservices.My问题是我只从webservice获得最后一个值,而且还在重复。我没有得到其他值。这是我的代码。请帮我 提前致谢

1 个答案:

答案 0 :(得分:0)

您正在将元素添加到arraylist而不清除LiveTrackArrayList.add(vdetails);

通过在f​​orloop

上方使用LiveTrackArrayList添加之前清除LiveTrackArrayList.clear();
  JSONArray array = object.getJSONArray("data");
here >  LiveTrackArrayList.clear();
    for (int i = 0; i < array.length(); i++) {