2014-04-17 37 views
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; 

} 

嗨,當它被添加到arraylist.I清除arraylist.But問題仍然exixts從WebService最後一個值是從符重複時,它被添加到一個ArrayList

價值被重複。

從web服務我的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」 } ]添加到ArrayList的 請幫我當 }

HB被重複四次?

請提出解決方法?

+0

請加VehicleDetails'的'實現。 – JimmyB

+0

是不是'VehicleDetails.vehicleNumber'被聲明爲'static'? – JimmyB

+0

public class VehicleDetails {\t \t public static String vehicleNo =「」; \t public static String vehicleId =「」; \t } – user3544129

回答

-1

你的問題是你重新創建一個單一的實例,並且因爲java是通過引用傳遞所有在arraylist點的最後一個實例的值。

爲了防止這個問題爲每個條目創建一個新的實例;

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

vehicleArrayList.add(vdetails); 

,並刪除這條線

VehicleDetails vdetails = null; 
+0

這顯然是錯誤的。 – JimmyB

+0

我測試了它,你爲什麼說錯了? – hister

+1

「,因爲java是通過引用傳遞所有在arraylist點的值在最後一個實例」是不正確的。 - 而不是你提出的OP已經有的代碼? – JimmyB

相關問題