在这里,我尝试将值存储在从JSON获得的类中,但后来无法从该类中检索这些数据。
int length = rnty.renter_id.size();当我再次初始化该类时返回0。但是在调试模式下,它表明数据已正确存储。
尝试{
JSONObject json_object= new JSONObject(json_string);
JSONArray json_array= json_object.getJSONArray("response");
int count = 0;
String renter_id,address,lat,longitu,rate;
renterMarkerInformation rntr= new renterMarkerInformation();
while(count<json_array.length()){
JSONObject jo= json_array.getJSONObject(count);
renter_id= jo.getString("renter_id");
address= jo.getString("address");
lat= jo.getString("latitude");
longitu= jo.getString("longitute");
rate=jo.getString("rateperhour");
rntr.setRenter_id(renter_id);
rntr.setAddress(address);
rntr.setLattitude(Double.parseDouble(lat));
rntr.setLongitude(Double.parseDouble(longitu));
rntr.setRate(Integer.parseInt(rate));
//LatLng sydney = new LatLng(Double.parseDouble(lat), Double.parseDouble(longitu));
//mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
count++;
}
} catch (JSONException e) {
e.printStackTrace();
}
renterMarkerInformation rnty= new renterMarkerInformation();
int length= rnty.renter_id.size();
for(int i=0;i<length;i++){
LatLng sydney = new LatLng(rnty.getLattitude(i), rnty.getLongitude(i));
mMap.addMarker(new MarkerOptions().position(sydney).title(rnty.getAddress(i)));
// mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney,19.4f));
}
**
这是我正在访问的另一个
**
公共类renterMarkerInformation {
ArrayList<String> renter_id= new ArrayList();
ArrayList<String> address= new ArrayList();
ArrayList<Double> lattitude= new ArrayList<>();
ArrayList<Double> longitude= new ArrayList<>();
ArrayList<Integer> rate= new ArrayList<>();
public void setRenter_id(String renter_id) {
this.renter_id.add(renter_id);
}
public void setAddress(String address){
this.address.add(address);
}
public void setLattitude(Double lattitude){
this.lattitude.add(lattitude);
}
public void setLongitude(Double longitude){
this.longitude.add(longitude);
}
public void setRate(Integer rate){
this.rate.add(rate);
}
public String getRenter_id(int index){
return renter_id.get(index);
}
public String getAddress(int index){
return address.get(index);
}
public Double getLattitude(int index){
return lattitude.get(index);
}
public Double getLongitude(int index){
return longitude.get(index);
}
public Integer getRate(int index){
return rate.get(index);
}
}
答案 0 :(得分:1)
变量的作用域一经尝试就结束,因为变量在try块中定义且仅具有块作用域。尝试在类级别声明对象并使用它。
class XYZ{
renterMarkerInformation rntr;
...
try {
rntr =new renterMarkerInformation();
}
...
LatLng sydney = new LatLng(rntr.getLattitude(i), rntr.getLongitude(i));
}