I would like to parse a json, I've managed to parse it in the onCreate but the app shows a blank screen for about 5-6 secs, so I've thought that i'll do some doInBackground and show a progressbar until the parsing is done.. but the app keeps on crashing with the OutOfBoundsExeption...
Here's my code :
public class MainActivity extends Activity {
private ArrayList<LatLng> markers = new ArrayList<LatLng>();
private LocationManager locationManager;
TextView text, distance, metric, warningTxt, speedTxt, kmh;
public double lat, lon;
private Location nearest;
private ArrayList<Location> cameras;
String answerstr;
public EditText editt;
public ImageView sign;
JSONObject jsonobject;
JSONArray jsonarray;
ProgressDialog progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
text = (TextView) findViewById(R.id.text);
new DownloadJSON().execute();
cameras = new ArrayList<Location>();
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
0, mlocListener);
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar = new ProgressDialog(MainActivity.this);
progressBar.setTitle("Çek 1.5 JSON");
progressBar.setMessage("Jsondan veriler çekiliyor...");
progressBar.setIndeterminate(false);
progressBar.show();
}
@Override
protected Void doInBackground(Void... params) {
jsonarray = JSONParser
.getJSONfromURL("https://dl.dropboxusercontent.com/s/84el6b3mpgat7uj/aggie.json");
try {
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject row;
row = jsonarray.getJSONObject(i);
Location currentLoc;
String camera;
double lat, lon;
camera = row.getString("name");
lat = row.getDouble("latitude");
lon = row.getDouble("longitude");
currentLoc = new Location(camera);
currentLoc.setLatitude(lat);
currentLoc.setLongitude(lon);
cameras.add(currentLoc);
}
} catch (NullPointerException ex) {
ex.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
progressBar.dismiss();
}
}
public void sortByLocation(Location mycurrentlocation) {
boolean swapped = true;
while (swapped) {
swapped = false;
for (int i = 1; i < cameras.size(); i++) {
if (cameras.get(i - 1).distanceTo(mycurrentlocation) > cameras
.get(i).distanceTo(mycurrentlocation)) {
Location tmp = cameras.get(i);
cameras.set(i, cameras.get(i - 1));
cameras.set(i - 1, tmp);
swapped = true;
}
}
}
}
public Location getNearest() {
nearest = cameras.get(0);
return nearest;
}
// handles the menu.
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
lat = loc.getLatitude();
lon = loc.getLongitude();
Location currentLocation = new Location("Current");
currentLocation.setLatitude(lat);
currentLocation.setLongitude(lon);
LatLng tmp = new LatLng(loc.getLatitude(), loc.getLongitude());
sortByLocation(currentLocation);
text.setText(getNearest().getProvider());
if (currentLocation.distanceTo(getNearest()) < 1000) {
text.setTextColor(Color.RED);
}
else {
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
And this is my logcat :
08-20 21:51:06.256: E/AndroidRuntime(26236): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
08-20 21:51:06.256: E/AndroidRuntime(26236): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
08-20 21:51:06.256: E/AndroidRuntime(26236): at java.util.ArrayList.get(ArrayList.java:308)
08-20 21:51:06.256: E/AndroidRuntime(26236): at com.finlaysmith.MotorcycleParking.MainActivity.getNearest(MainActivity.java:190)
08-20 21:51:06.256: E/AndroidRuntime(26236): at com.finlaysmith.MotorcycleParking.MainActivity$MyLocationListener.onLocationChanged(MainActivity.java:217)
08-20 21:51:06.256: E/AndroidRuntime(26236): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
08-20 21:51:06.256: E/AndroidRuntime(26236): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
08-20 21:51:06.256: E/AndroidRuntime(26236): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
08-20 21:51:06.256: E/AndroidRuntime(26236): at android.os.Handler.dispatchMessage(Handler.java:102)
08-20 21:51:06.256: E/AndroidRuntime(26236): at android.os.Looper.loop(Looper.java:136)
08-20 21:51:06.256: E/AndroidRuntime(26236): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-20 21:51:06.256: E/AndroidRuntime(26236): at java.lang.reflect.Method.invokeNative(Native Method)
08-20 21:51:06.256: E/AndroidRuntime(26236): at java.lang.reflect.Method.invoke(Method.java:515)
08-20 21:51:06.256: E/AndroidRuntime(26236): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-20 21:51:06.256: E/AndroidRuntime(26236): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)