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被重複四次?
請提出解決方法?
請加VehicleDetails'的'實現。 – JimmyB
是不是'VehicleDetails.vehicleNumber'被聲明爲'static'? – JimmyB
public class VehicleDetails {\t \t public static String vehicleNo =「」; \t public static String vehicleId =「」; \t } – user3544129